CSU 1547 背包

来源:互联网 发布:淘宝 省钱快报 编辑:程序博客网 时间:2024/06/04 19:35

A - A
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu
Submit Status Practice CSU 1547

Description

Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(these rectangles can't rotate). please calculate the minimum m satisfy the condition.

Input

There are some tests ,the first line give you the test number. 
Each test will give you a number n (1<=n<=100)show the rectangles number .The following n rows , each row will give you tow number a and b. (a = 1 or 2 , 1<=b<=100).

Output

Each test you will output the minimum number m to fill all these rectangles.

Sample Input

231 22 22 331 21 21 3

Sample Output

74



题意:给你一些小块只有1*X,或者2*X,问使用一个2*M的大方块装下这些小方块,M只至少要多大?


题解:2*X直接累加,1*X的加起来和为V,创建一个容量为V/2的背包,尽可能的装进这个背包最小代价就是V-DP[V/2]啦。



#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<set>#include<vector>#include<map>using namespace std;#define N 20000struct point{int x,y;point (int _x,int _y):x(_x),y(_y){}point(){}};vector<point>eg;int dp[N];int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifint n,t,x,y;scanf("%d",&t);while(t--){eg.clear();memset(dp,0,sizeof(dp));int sum=0,v=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d",&x,&y);if(x==2){sum+=y;}else{v+=y;eg.push_back(point(x,y));}}for(int i=0;i<eg.size();i++){for(int j=v/2;j>=eg[i].y;j--){dp[j]=max(dp[j],dp[j-eg[i].y]+eg[i].y);}}printf("%d\n",v-dp[v/2]+sum);}return 0;}


0 0