http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category= &problem=1631
Main topic:
With a bunch of numbers divided into two A, b two piles, a pile of M a bunch of n. The maximum and minimum value of the product of the sum of the sum of a heap number and the sum of the B-stack numbers
Practice:
Since there are negative numbers, ( -50,50) We first add each number to 100,dp[i][j] to represent a J with the first I number, then the state transfer equation is (Dp[i][j]=dp[i-1][j-num[k]). K is 1 to M+n, enumeration K can be obtained dp[i][j] existence situation. Finally, find the maximum and minimum values in the solution space.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intdp[ -][20010],num[ the];intMain () {intn,m; while(~SCANF ("%d%d",&n,&m)) { intall=n+m; intsum=0; for(intI=1; i<=all;i++) {scanf ("%d",&Num[i]); Sum+=Num[i]; Num[i]=num[i]+ -; } if(n>m) {swap (N,M); } memset (DP,0,sizeof(DP)); dp[0][0]=1; for(intI=1; i<=all;i++){ for(intJ=min (n,i);j>0; j--){ for(intk=sum+ (All) * -; k>=num[i];k--){ if(dp[j-1][k-Num[i]]) {Dp[j][k]=1; } } } } //cout<<dp[1][1]<<endl; intMini=1e9; intmaxi=-1e9; for(intI=0; i<=10000; i++){ if(Dp[n][i]) {intx=i-n* -; //cout<<x<<endl;Mini=min (mini,x* (sum-x)); Maxi=max (maxi,x* (sum-x)); }} printf ("%d%d\n", Maxi,mini); } return 0;}
Use the sum of n in m coins uva10690