Test instructions: A bag of coins for two people, either fair points, or unfair, if it can be fair, output 0, otherwise the output is divided into two halves of the minimum gap.
Idea: will provide the whole bag of money to take half of the total to carry out 01 backpack, if can be separated out, is the best method. Otherwise, the bag with a half-price package can be loaded with the total value of the coin that one person can get the most money, and the rest of the money minus the gross value of the package. (Just a little consideration of the odd/even number of questions)
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <cmath>5 using namespacestd;6 Const intn=100000;7 intW[n], dp[n];8 9 voidCalintNintCNT)Ten { One intm; A if(cnt&1==1) m = (cnt>>1) +1;//Odd - ElseM=cnt>>1; - the for(intI=0; i<n; i++) - for(intJ=m; j>=w[i]; j--) -Dp[j]=max (dp[j-w[i]]+W[i], dp[j]); - + if(cnt&1==1)//Odd - { + if(dp[m]==m) cout<<"1"<<endl;//the fairest A Elsecout<<cnt-dp[m]-dp[m]<<Endl; at } - Else - { - if(dp[m]==m) cout<<"0"<<endl;//the fairest - Elsecout<<cnt-dp[m]-dp[m]<<Endl; - } in - } to + intMain () - { the //freopen ("Input.txt", "R", stdin); * intT, N, CNT; $Cin>>T;Panax Notoginseng while(t--) - { theMemset (DP,0,sizeof(DP)); +Memset (W,0,sizeof(w)); ACin>>N; theCnt=0; + for(intI=0; i<n; i++) - { $scanf"%d",&w[i]); $cnt+=W[i]; - } - if(n==0) thecout<<"0"<<Endl; - Else if(n==1)Wuyicout<<w[0]<<Endl; the Else if(n==2) -Cout<<abs (w[0]-w[1]) <<Endl; Wu Else - cal (n, CNT); About } $ return 0; -}
AC Code
UVA 562 Dividing coins split coin (01 backpack, simple variant)