Lightoj-1147-tug of War (pressure DP)

Source: Internet
Author: User

1147-tug of War
PDF (中文版) statisticsforum
Time Limit:4 second (s) Memory limit:32 MB
A tug of war is to being arranged at the local office picnic. For the tug of war, the picnickers must is divided into and teams. Each person must is on a team or the other; The number of people on the teams must not differ by more than 1; The total weight of the people in each team should is as nearly equal as possible.

Input
Input starts with an integer T (≤100), denoting the number of test cases.

The first line of all case was a blank line. The next line of input contains a integer n (2≤n≤100), the number of people at the picnic. n lines follow. The first line gives the weight for person 1; The second the weight of person 2; And so on. Each weight are an integer between 1 and 100000. The summation of the weights of the people in a case would not exceed 100000.

Output
For each case, print the case number and the total number weights of the people in the 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

Problem-solving ideas: The general idea of this problem is 01 backpack. But there is one condition for attention, that is, the difference between the two teams is 1. So it's not just a 01-pack, we figure out the closest to the average, and we have to judge if he can N/2 | | N/2+1 personal composition. So the problem in the basic template of the 01 backpack to calculate the number of people can be composed of several persons to record. A look at the need to record state is basically state compression is not wrong

Because n<=100, so to record up to 50 bits, to use a long long to save

ll dp[w] = m, W is weight, M is a binary record. The I-bit representation of M is made up of i-1 individuals (since the initialization must be dp[0]=1, but dp[0] although there is 1, but it is no one constitutes him), then if 110, it can be composed of 1 persons, also can be composed of 2 persons.

Fully available transfer equation Dp[w] = dp[w]| (dp[w-arr[i]]<<1);

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;ll dp[50010];ll arr[ the];BOOLjudge (ll X,ll N) {if(n%2==0){        intnum = n/2; return(x& (1ll<<num))! =0; }Else{        intnum=n/2; return(x& (1ll<<num))! =0|| (x& (1ll<< (num+1)))!=0; }    }intMain () {ll sum,t,n; scanf ("%lld",&T);  for(LL t=1; t<=t;t++) {memset (DP,0,sizeof(DP)); scanf ("%lld",&N); Sum=0;  for(LL i=0; i<n;i++) {scanf ("%lld",&Arr[i]); Sum+=Arr[i]; } dp[0] =1;  for(LL i=0; i<n;i++){             for(LL j=sum/2; j>=arr[i];j--) {Dp[j]= dp[j]| (dp[j-arr[i]]<<1); }        }         for(LL i=sum/2; i>=0; i--){            if(judge (Dp[i],n)) {printf ("Case %lld:%lld%lld\n", t,i,sum-i);  Break; }        }            }     } 

Lightoj-1147-tug of War (pressure DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.