Problem A: Rectangle
Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 207 Solved: 46
[ Submit][ Status][ Web Board]
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
2
3
1 2
2 2
2 3
3
1 2
1 2
1 3
Sample Output
7
#include<algorithm>
#include<iostream>
#include<limits.h>
#include<stdlib.h>
#include<string.h>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<bitset>
#include<cctype>
#include<math.h>
#include<string>
#include<time.h>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#define LL long long
using namespace std;
const LL mod = 1e9 + 7;
const double PI = acos(-1.0);
const double E = exp(1);
const int M = 110;
int aa[M];
int dp[M * M];
int main()
{
int t;
int n;
cin >> t;
while( t-- ){
cin >> n;
int a, b;
int ans = 0;
int flag = 0;
int sum = 0;
while( n-- ){
cin >> a >> b;
if(a == 2)
ans += b;
else{
aa[flag++] = b;
sum += b;
}
}
memset(dp,0,sizeof(dp));
for(int i = 0; i < flag; ++i)
for(int j = sum / 2; j >= aa[i]; --j)
dp[j] = max(dp[j],dp[j - aa[i]] + aa[i]);
ans += sum - dp[sum / 2];
cout << ans << endl;
}
return 0;
}