标签:class last exist 优化 mem round 时间 没有 call
Description
Input
Output
Sample Input
2 9 1 2 2 2 2 3 3 3 1 1 1
Sample Output
Case 1: 29 Case 2: 1
Source
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define R register 5 6 int T,n,last[201],color[201],pre[201],num[201],s[201][201][201]; 7 8 void memorysearch(int l,int r,int k){ 9 if (s[l][r][k]||r<l) return; 10 if (l==r){ 11 s[l][r][k]=(num[l]+k)*(num[l]+k); 12 return; 13 } 14 memorysearch(l,r-1,0); 15 s[l][r][k]=s[l][r-1][0]+(num[r]+k)*(num[r]+k); 16 for (R int p=pre[r];p>=l;p=pre[p]){ 17 memorysearch(l,p,k+num[r]); 18 memorysearch(p+1,r-1,0); 19 s[l][r][k]=std::max(s[l][r][k],s[l][p][k+num[r]]+s[p+1][r-1][0]); 20 } 21 } 22 23 int main(){ 24 scanf("%d",&T); 25 for (R int round=1;round<=T;++round){ 26 memset(s,0,sizeof s);n=0; 27 memset(pre,0,sizeof pre); 28 memset(last,0,sizeof last); 29 int size;scanf("%d",&size); 30 for (R int i=1;i<=size;++i){ 31 int c;scanf("%d",&c); 32 if (c==color[n]) ++num[n]; 33 else color[++n]=c,num[n]=1; 34 } 35 for (R int i=1;i<=n;++i) pre[i]=last[color[i]],last[color[i]]=i; 36 memorysearch(1,n,0); 37 printf("Case %d: %d\n",round,s[1][n][0]); 38 } 39 return 0; 40 }
标签:class last exist 优化 mem round 时间 没有 call
原文地址:http://www.cnblogs.com/hyheng/p/7103652.html