标签:print spec har lin memset ant ble www player
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 2482 Accepted Submission(s): 1126
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[(1<<21)+55][9]; int G,B,S,v[25][10],a[10]; int main(){ while(scanf("%d%d%d",&G,&B,&S),G+B+S){ memset(v,0,sizeof(v)); memset(a,0,sizeof(a)); for(int i=0;i<B;++i) { int x,n; for(scanf("%d",&n);n--;){ scanf("%d",&x); v[i][x]++; a[x]++; a[x]%=S; } } int mm=(1<<B)-1; for(int i=1;i<=G;++i) dp[0][i]=a[i]; dp[0][0]=0; for(int i=1;i<=mm;++i) dp[i][0]=-10000000; for(int i=0;i<=mm;++i) for(int j=0;j<B;++j) { int x=(1<<j); if(!(i&x)) { int gs=0; for(int k=1;k<=G;++k) { a[k]=dp[i][k]-v[j][k]; while(a[k]<0) { gs++; a[k]+=S; } dp[i^x][k]=a[k]; } if(gs>0) dp[i^x][0]=max(dp[i^x][0],gs+dp[i][0]); else dp[i^x][0]=max(dp[i^x][0],-dp[i][0]); } } printf("%d\n",dp[mm][0]); } }
这个是消耗时间换空间的
http://www.cnblogs.com/kuangbin/p/3416196.html
/* *********************************************** Author :kuangbin Created Time :2013-11-9 12:55:09 File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1009.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; int c[22][20]; int b[20]; int d[20]; int dp[1<<22]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int G,B,S; int n,t; while(scanf("%d%d%d",&G,&B,&S) == 3) { if(G == 0 && B == 0 && S == 0)break; memset(c,0,sizeof(c)); for(int i = 0;i < B;i++) { scanf("%d",&n); while(n--) { scanf("%d",&t); c[i][t]++; } } dp[0] = 0; int tot = (1<<B); for(int i = 1;i < tot;i++) { dp[i] = -10000000; for(int j = 1;j <= G;j++) b[j] = 0; for(int j = 0;j < B;j++) if((i&(1<<j)) == 0) { for(int k = 1;k <= G;k++) { b[k] += c[j][k]; while(b[k] >= S) b[k] -= S; } } //cout<<i<<"*"<<endl; //for(int j = 1;j <= G;j++) //cout<<b[j]<<endl; for(int j = 0;j < B;j++) if(i & (1<<j)) { for(int k = 1;k <= G;k++) d[k] = b[k]; int cnt = 0; for(int k = 1;k <= G;k++) { d[k] += c[j][k]; while(d[k] >= S) { d[k] -= S; cnt++; } } if(cnt > 0)dp[i] = max(dp[i],cnt + dp[i^(1<<j)]); else dp[i] = max(dp[i],cnt - dp[i^(1<<j)]); } } printf("%d\n",dp[tot-1]); } return 0; }
标签:print spec har lin memset ant ble www player
原文地址:http://www.cnblogs.com/mfys/p/7625335.html