Difficulties of ant financial (8)
Time Limit: 2000 MS | memory limit: 65535 KB
Difficulty: 5
Description
Ant is an antique enthusiast who has collected a lot of bottles and cans.
One day, he will arrange his babies in one line and place them on a booth with a length of L.
It is known that he has n pieces of baby, each of which has a width of W. Because these bottles and cans have a special shape, they need at least x width for placement, (only the width of X is required for placement, and the width is still w after placement) Now we know the width of wi for each item and the width XI required for placing them. Please help ant compute. On this booth, he can put at most a wide place.
Input
There are multiple groups of test data.
For each group of test data:
Line 1: n l represents n pieces of baby, with the length of the booth being L (n <1000, L <10000)
Next there are n rows, each line has two integer wi Xi representing the width of the I-part baby and the width required for placement. (0 <wi <= xi <10000 ).
Output
Output the maximum width that ant can pose.
Sample Input
3 10
2 3
3 4
4 7
Sample output
9
Prompt
Ant financial's difficult problem has ended. I wish you a happy life!
Source
Ant Series
Uploaded
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 10001;18 struct node{19 int w,x;20 bool operator<(const node &u) const{21 return x-w > u.x - u.w;22 }23 };24 node p[maxn];25 int n,L,dp[maxn];26 int main() {27 while(~scanf("%d %d",&n,&L)){28 for(int i = 1; i <= n; ++i)29 scanf("%d %d",&p[i].w,&p[i].x);30 sort(p+1,p+n+1);31 memset(dp,0,sizeof(dp));32 int ans = 0;33 for(int i = 1; i <= n; ++i){34 for(int j = L; j >= p[i].w; --j){35 if(j-p[i].w <= L - p[i].x) dp[j] = max(dp[j],dp[j-p[i].w]+p[i].w);36 ans = max(ans,dp[j]);37 }38 }39 printf("%d\n",ans);40 }41 return 0;42 }
View code
Nyist 749 ant financial problems (8)