http://acm.hdu.edu.cn/showproblem.php?pid=1171
//1305008 2009-04-24 17:36:03 Accepted 1171 2015MS 1260K 838 B C++ no way //1305033 2009-04-24 17:42:02 Accepted 1171 796MS 760K 664 B C++ no way #include<iostream> #include<cmath> using namespace std; int outs[250002]; int main() { int n; while(cin>>n && n>=0)//A test case starting with a negative integer terminates input //and this test case is not to be processed 这句话让我错了很多次 ,值得注意 { int i,j,k,s,N=0,t; int w[51],num[51]; for(i=1;i<=n;i++) { scanf("%d%d",&w[i],&num[i]); N += w[i] * num[i]; } t = N/2; //取其一半
for(i=0;i<=N;i++) outs[i] = 0; for(i=0;i<=num[1];i++) outs[i*w[1]] = 1;
for(i=2;i<=n;i++) { for(j=0;j<=t;j++) { for(k=0,s=0;s<=num[i] && k+j<=t; k+=w[i],s++) if( outs[j] == 1) outs[k+j] = 1; } } for(i = t; i>=0 ;i--) if(outs[i] !=0 ) break; printf("%d %d\n",i + N-2*i ,i); } return 0; }
|