lightoj 1147 - Tug of War
2016-10-11 19:24
387 查看
A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight
of the people on each team should be as nearly equal as possible.
The first line of each case is a blank line. The next line of input contains an integer n (2 ≤ n ≤ 100), the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight
of person 2; and so on. Each weight is an integer between 1 and 100000. The summation of all the weights of the people in a case will not exceed 100000.
题意是给你一些人的重量,让你把他们分成人数差最大为1的两组,要求保证两组重量的差最小,先输出小的那个。
dp[i] = x,x是一个二进制数,第k位是1的话表示有k个人的重量为i。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int main(void)
{
int T,n,i,j;
int a[110];
LL dp[100010];
scanf("%d",&T);
int cas = 1;
while(T--)
{
scanf("%d",&n);
int sum = 0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum += a[i];
}
memset(dp,0,sizeof(dp));
int V = sum/2;
dp[0] = 1;
for(i=1;i<=n;i++)
{
for(j=V;j>=a[i];j--)
{
dp[j] |= (dp[j-a[i]]<<1);
}
}
for(i=V;i>=0;i--)
{
if(dp[i]&(1LL<<(n+1)/2)||dp[i]&(1LL<<(n/2)))
{
printf("Case %d: %d %d\n",cas++,i,sum-i);
break;
}
}
}
return 0;
}
of the people on each team should be as nearly equal as possible.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.The first line of each case is a blank line. The next line of input contains an integer n (2 ≤ n ≤ 100), the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight
of person 2; and so on. Each weight is an integer between 1 and 100000. The summation of all the weights of the people in a case will not exceed 100000.
Output
For each case, print the case number and the total number weights of the people in two teams. If the weights differ, print the smaller weight first.Sample Input | Output for Sample Input |
2 3 100 90 200 4 10 15 17 20 | Case 1: 190 200 Case 2: 30 32 |
dp[i] = x,x是一个二进制数,第k位是1的话表示有k个人的重量为i。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int main(void)
{
int T,n,i,j;
int a[110];
LL dp[100010];
scanf("%d",&T);
int cas = 1;
while(T--)
{
scanf("%d",&n);
int sum = 0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum += a[i];
}
memset(dp,0,sizeof(dp));
int V = sum/2;
dp[0] = 1;
for(i=1;i<=n;i++)
{
for(j=V;j>=a[i];j--)
{
dp[j] |= (dp[j-a[i]]<<1);
}
}
for(i=V;i>=0;i--)
{
if(dp[i]&(1LL<<(n+1)/2)||dp[i]&(1LL<<(n/2)))
{
printf("Case %d: %d %d\n",cas++,i,sum-i);
break;
}
}
}
return 0;
}
相关文章推荐
- LightOJ 1147 Tug of War(状态压缩)
- lightoj 1147 - Tug of War 动态规划
- Light oj 1147 - Tug of War(状压dp)
- Light OJ 1147 Tug of War (背包+状压)
- POJ 2576 / Light OJ 1147 Tug of War 状态压缩DP
- 第六届山东省ACM竞赛 A题 Nias and Tug-of-War(java)
- POJ2576 Tug of War (二维的01背包)
- zoj 1880 - Tug of War
- ZCMU-Tug of War
- UVA - 10032 Tug of War (二进制标记+01背包)
- POJ 2576 Tug of War 二维背包 OR 随机化
- UVA 10032 Tug of War
- POJ 2576 Tug of War 二维费用背包
- ZOJ - 1880 Tug of War
- Tug of War(严格限制数量的二维费用背包)
- POJ 2576 Tug of War二维背包恰好装满.
- UVa 10032 - Tug of War
- ZOJ1880 POJ2576 Tug of War,DP
- sdut 3251 Nias and Tug-of-War 模拟
- poj2576 Tug of War(二维费用背包)