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 |
DP//poj2576 #include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; int n,m,v,wei,f[105]; int a[22505][55]; int b[23505][55]; void into() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&f[i]); wei+=f[i]; } v=wei/2;m=n/2; if(n%2!=0)m++; } void js() { for(int i=1;i<=n;i++) for(int j=v;j>=f[i];j--) for(int k=1;k<=m;k++) { if(a[j-f[i]][k-1]+1>a[j][k]) { a[j][k]=a[j-f[i]][k-1]+1; b[j][k]=b[j-f[i]][k-1]+f[i]; } else if((a[j-f[i]][k-1]+1==a[j][k])&&(b[j-f[i]][k-1]+f[i]>b[j][k]))b[j][k]=b[j-f[i]][k-1]+f[i]; } if(wei-b[v][m]>b[v][m])printf("%d %d\n",b[v][m],wei-b[v][m]); else printf("%d %d\n",wei-b[v][m],b[v][m]); } int main() { into(); js(); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator