1959 Tug of war competition
time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingView Run ResultsTitle Description
Description
A school held a tug-of-war competition, all the people were divided into two groups, each must (and only can) in one of the groups, requiring two groups of the number of people not to exceed 1, and two groups of all body weight together as close as possible.
Enter a description
Input Description
The 1th line of data is an n, representing the total number of people participating in the tug-of-war, n<=100, and the next n lines represent the 1th to nth person's weight, and each person's weight is an integer (1<=weight<=450).
Output description
Output Description
Contains two integers: the weight of each of the two groups, separated by a single space. Note If these two numbers are not equal, put the small one in front of the output.
Sample input
Sample Input
3
100
90
200
Sample output
Sample Output
190 200
Data range and Tips
Data Size & HintCategory labels
Tags Click here to expandDynamic planning
/*the bool array determines whether the first I item can reach J this mass*/#include<iostream>using namespacestd; #include<cstdio>BOOLf[101][45001]={false};intn,w[101];intMain () {intk,sum=0, Summ; scanf ("%d",&N); if(n%2==1) k= (n+1)/2;/*K,summ, the purpose of this is to take a large number of N/2 when n is odd, and more accurately for subsequent results*/ Elsek=n/2; for(intI=1; i<=n;++i) {scanf ("%d",&W[i]); Sum+=W[i]; } f[0][0]=f[1][0]=true; if(sum%2==1) summ= (sum+1)/2; Elsesumm=sum/2; for(intI=1; i<=n;++i)/*In addition to the DP equation, the rest is similar to the 01 backpack*/ for(intj=summ;j>=w[i];--j) for(intq=k;q>=1;--q) f[q][j]=f[q][j]| | f[q-1][j-w[i]];/*The current Q person, can reach the quality of J, is to see q-1 people can reach the quality and current quality of this person*/ intT; for(intj=summ;j>=0;--j) {if(F[k][j])/*Note that f is an array of type bool, J is the last value*/{T=J; Break; }} cout<<min (T,SUM-T) <<" "<<max (T,SUM-T) <<Endl; return 0; }
View Code
Codevs 1959 Tug-of-war contest--to judge that the backpack is filled with N/2 items