Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0’’. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For each colletcion, output ‘’Collection #k:’’, where k is the number of the test case, and then either `‘Can be divided.’’ or ``Can’t be divided.’’.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.
Collection #2:
Can be divided.
#include
#include
using namespace std;
int f[100000];
int main()
{
int a[7];int t=1;
while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]&&a[1]+a[2]+a[3]+a[4]+a[5]+a[6])
{
int s=0;
for(int i=1;i<=6;i++)
s+=a[i]*i;
if(s%2==1)
{
cout<<"Collection #"<=k*i;j--)
f[j]=max(f[j],f[j-k*i]+k*i);
}
for(int j=s/2;j>=a[i]*i;j--)
f[j]=max(f[j],f[j-a[i]*i]+a[i]*i);
}
cout<<"Collection #"<
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 – the total number of different facilities). The next N lines contain an integer V (0
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40
#include
#include
using namespace std;
const int N=90000;
int main()
{
int v[N],m[N],f[N];
while(1)
{
memset(f,0, sizeof(f));//#include
int n;
cin>>n;
int s=0;
if(n<0)
break;
for(int i=1; i<=n; i++)
{
cin>>m[i]>>v[i];
s+=m[i]*v[i];
}
for(int i=1; i<=n; i++)
{
for(int k=v[i];k>0;k--)//把一样的物品拆成了一个一个用01背包做
for(int j=s/2; j>=m[i]; j--)
f[j]=max(f[j],f[j-m[i]]+m[i]);//#include
}
cout<
“Fat and docile, big and dumb, they look so stupid, they aren’t much
fun…”
- Cows with Guns by Dana Lyons
The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si’s and, likewise, the total funness TF of the group is the sum of the Fi’s. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
Input
* Line 1: A single integer N, the number of cows
Sample Input
5
-5 7
8 -6
6 -3
2 1
-8 -5
Sample Output
8
Hint
OUTPUT DETAILS:
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
#include
#include
#include
#include
using namespace std;
int dp[200005];
int main()
{
int n,t=0;
cin>>n;
int s[105],f[105];
for(int i=1;i<=n;i++)
cin>>s[i]>>f[i];
for(int i = 0; i < 200005; i++)
dp[i]=-1000000;
dp[100000]=0;
for(int i = 1; i <= n; i++)
{
if(s[i] > 0 || f[i] > 0)
{
if(s[i] > 0)
{
for(int j = 200000; j >= s[i]; j--)
{
dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
}
}
else
{
for(int j = 0; j <= 200000+ s[i]; j++)
{
dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
}
}
}
}
for(int i=100000;i<=200000;i++)
if(dp[i]>=0)t = max(t, dp[i] + i -100000);
cout<