Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我用背包的算法,但总是WA~~~哪位高手能帮我看一下,我泄了什么了吗??错哪了~~~~#include <iostream.h> #define k 500 bool a[1000],v[1000][25]; int b[1000][25][23],sum[1000][25]; int d[210],p[210],c[210],e[210],play[210]; int n,m,dmax,pmax; void sort(int h,int l); void done(void); void work(int pp[210]); void print(int num); void main() { int i,j,step,h; step=0; cin>>n>>m; while (n>0) { step++; cout<<"Jury #"<<step<<endl; for(i=1;i<=n;i++) { play[i]=i; cin>>d[i]>>p[i]; c[i]=d[i]-p[i]; } for(i=1;i<=n;i++) e[i]=d[i]+p[i]; sort(1,n); for(i=1;i<=n;i++) b[i][0][1]=play[i]; done(); work(play); cout<<"Best jury has value "<<pmax<<" for prosecution and value "<<dmax<<" for defence:"<<endl; for(i=1;i<m;i++) for(j=i+1;j<=m;j++) if (e[j]<e[i]) { h=e[j]; e[j]=e[i]; e[i]=h; } for(i=1;i<=m;i++) cout<<" "<<e[i]; cout<<endl; cout<<endl; cin>>n>>m; } } void sort(int h,int l) { int i,j,mid,g; i=h; j=l; mid=c[(i+j)/2]; do { while ((c[i]<mid) && (i<l)) i++; while ((c[j]>mid) && (j>h)) j--; if (i<=j) { g=c[i]; c[i]=c[j]; c[j]=g; g=e[i]; e[i]=e[j]; e[j]=g; g=play[i]; play[i]=play[j]; play[j]=g; i++; j--; } }while (i<=j); if (i<l) sort(i,l); if (j>h) sort(h,j); } void done(void) { int i,j,l,h,num,u,min,max,z; for(i=100;i<=1000;i++) { a[i]=false; for(j=1;j<=23;j++) { sum[i][j]=0; v[i][j]=false; } } num=c[1]+k; b[num][1][1]=1; v[num][1]=true; sum[num][1]+=e[1]; a[num]=true; h=num; l=num; min=num; max=num; for(i=2;i<=n;i++) { if (c[i]<=0) { for(j=h;j<=l;j++) if (a[j]) { if (j+c[i]<min) min=j+c[i]; if (j+c[i]>max) max=j+c[i]; for(u=m;u>0;u--) if ((v[j][u]) && (!v[j+c[i]][u+1])) { v[j+c[i]][u+1]=true; for(z=1;z<=u;z++) b[j+c[i]][u+1][z]=b[j][u][z]; b[j+c[i]][u+1][u+1]=i; sum[j+c[i]][u+1]=sum[j][u]+e[i]; } else if ((v[j][u]) && (v[j+c[i]][u+1]) && (sum[j][u]+e[i]>sum[j+c[i]][u+1])) { for(z=1;z<=u;z++) b[j+c[i]][u+1][z]=b[j][u][z]; b[j+c[i]][u+1][u+1]=i; sum[j+c[i]][u+1]=sum[j][u]+e[i]; } } } else for(j=l;j>=h;j--) if (a[j]) { if (j+c[i]<min) min=j+c[i]; if (j+c[i]>max) max=j+c[i]; for(u=m;u>0;u--) if ((v[j][u]) && (!v[j+c[i]][u+1])) { v[j+c[i]][u+1]=true; for(z=1;z<=u;z++) b[j+c[i]][u+1][z]=b[j][u][z]; b[j+c[i]][u+1][u+1]=i; sum[j+c[i]][u+1]=sum[j][u]+e[i]; } else if ((v[j][u]) && (v[j+c[i]][u+1]) && (sum[j][u]+e[i]>sum[j+c[i]][u+1])) { for(z=1;z<=u;z++) b[j+c[i]][u+1][z]=b[j][u][z]; b[j+c[i]][u+1][u+1]=i; sum[j+c[i]][u+1]=sum[j][u]+e[i]; } } if (c[i]<0) for(j=h;j<=l;j++) if (a[j]) a[j+c[i]]=true; if (c[i]>0) for(j=l;j>=h;j--) if (a[j]) a[j+c[i]]=true; num=c[i]+k; if (num>max) max=num; if (num<min) min=num; a[num]=true; if (((v[num][1]) && (sum[num][1]<e[i])) || (!v[num][1])) { v[num][1]=true; sum[num][1]=e[i]; b[num][1][1]=i; } h=min; l=max; if (h<100) h=100; if (l>800) l=800; } } void work(int pp[210]) { int i; for(i=0;i<401;i++) if ((v[k+i][m]) || (v[k-i][m])) break; if (sum[k+i][m]>sum[k-i][m]) print(k+i); else print(k-i); pmax=0; dmax=0; for(i=1;i<=m;i++) { e[i]=b[e[i]][0][1]; pmax+=p[e[i]]; dmax+=d[e[i]]; } } void print(int num) { int i; for(i=1;i<=m;i++) e[i]=b[num][m][i]; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator