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> #include <algorithm> using namespace std; int n, m; int d[201], p[201], sub[201], add[201]; struct node { bool exist; int path[21]; int sum; }f[21][801]; int main() { int i, j, k, t=0; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) { for (i=1; i<=n; i++) { scanf("%d %d", &d[i], &p[i]); sub[i] = d[i]-p[i]+20; add[i] = d[i]+p[i]; } memset(f, 0, sizeof(f)); f[0][0].exist = true; int x = 40*m; /*DP过程*/ for (i=1; i<=n; i++) { for (j=m; j>=0; j--) { for (k=0; k<=x; k++) { if (f[j][k].exist) { if (k-sub[i]>=0 && j-1>=0 && f[j-1][k-sub[i]].exist && f[j][k].sum <= f[j-1][k-sub[i]].sum+add[i]) { f[j][k] = f[j-1][k-sub[i]]; f[j][k].sum += add[i]; f[j][k].path[j] = i; } } else if (k-sub[i]>=0 && j-1>=0 && f[j-1][k-sub[i]].exist) { f[j][k] = f[j-1][k-sub[i]]; f[j][k].sum += add[i]; f[j][k].path[j] = i; } } } } /*寻找最优*/ int z = 20*m; for (i=0; i<=z; i++) { if (f[m][z+i].exist || f[m][z-i].exist) { if (f[m][z+i].exist) { j = z+i; if (f[m][z-i].exist && f[m][z-i].sum > f[m][z+i].sum) { j = z-i; } } else { j = z-i; } printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence:\n", ++t, (f[m][j].sum+i)>>1, (f[m][j].sum-i)>>1); sort(f[m][j].path+1,f[m][j].path+m+1); for (k=1; k<=m; k++) { printf(" %d", f[m][j].path[k]); } printf("\n\n"); break; } } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator