SOJ 2013年每周一赛第⑨场 D
| 1003. Bookshelf | ||
|
Description
When Farmer John isn't milking cows, stacking haybales, lining up his cows, or building fences, he enjoys sitting down with a good book. Over the years, he has collected N books (1 <= N <= 2,000), and he wants to build a new set of bookshelves to hold them all. Each book i has a width W(i) and height H(i). The books need to be added to a set of shelves in order; for example, the first shelf should contain books 1...k for some k, the second shelf should start with book k+1, and so on. Each shelf can have a total width of at most L (1 <= L <= 1,000,000,000). The height of a shelf is equal to the height of the tallest book on that shelf, and the height of the entire set of bookshelves is the sum of the heights of all the individual shelves, since they are all stacked vertically. Please help FJ compute the minimum possible height for the entire set of bookshelves. Input
Line 1: Two space-separated integers: N and L. Output
Line 1: The minimum possible total height for the set of bookshelves. Sample Input
Copy sample input to clipboard
5 10 5 7 9 2 8 5 13 2 3 8 Sample Output
21 Hint
There are 3 shelves, the first containing just book 1 (height 5, width 7), the second containing books 2..4 (height 13, width 9), and the third containing book 5 (height 3, width 8). Problem Source: 2013年每周一赛第⑨场
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 #define INF 0x7fffffff 15 #define maxm 1001 16 #define mp make_pair 17 #define pb push_back 18 #define rep(i,n) for(int i = 0; i < (n); i++) 19 #define re return 20 #define fi first 21 #define se second 22 #define sz(x) ((int) (x).size()) 23 #define all(x) (x).begin(), (x).end() 24 #define sqr(x) ((x) * (x)) 25 #define sqrt(x) sqrt(abs(x)) 26 #define y0 y3487465 27 #define y1 y8687969 28 #define fill(x,y) memset(x,y,sizeof(x)) 29 30 typedef vector<int> vi; 31 typedef long long ll; 32 typedef long double ld; 33 typedef double D; 34 typedef pair<int, int> ii; 35 typedef vector<ii> vii; 36 typedef vector<string> vs; 37 typedef vector<vi> vvi; 38 39 template<class T> T abs(T x) { re x > 0 ? x : -x; } 40 41 const int maxn = 2005; 42 43 int n, m; 44 int h[maxn], w[maxn]; 45 int dp[maxn]; 46 int main(){ 47 while (~scanf("%d%d", &n, &m)){ 48 for (int i = 0; i <= n; i++)dp[i] = INF; 49 dp[0] = 0; 50 for (int i = 1; i <= n; i++){ 51 scanf("%d%d", &h[i], &w[i]); 52 int sum = 0, x = 0; 53 for (int j = i; j >= 0; j--){ 54 if (sum + w[j] <= m)sum += w[j]; 55 else break; 56 x = max(x, h[j]); 57 dp[i] = min(dp[i], dp[j - 1] + x); 58 } 59 } 60 printf("%d\n", dp[n]); 61 } 62 return 0; 63 }
|

浙公网安备 33010602011771号