标签:des c style class blog code
Time Limit: 15000MS | Memory Limit: 30000K | |
Total Submissions: 8825 | Accepted: 3381 | |
Case Time Limit: 5000MS |
Description
Input
Output
Sample Input
2 6 6 5 1 4 4 6 2 2 3 6 6 4 6 5 4 3 3 6 1 6 2 6 4
Sample Output
3 4
Source
#include <iostream> #include <cstring> #include <cmath> #include <cstdio> #include <algorithm> #include <cstdlib> #define N 200 using namespace std; int status[N*10],a[N],b[N/10],dp[N][N*10],map[N][N]; int cou[N*10]; struct num { int y,next; }d[N*10*N*10]; int e[N*10]; int n,m,k,Top,bian; int main() { //freopen("data.txt","r",stdin); void pre_dp(); int get_dp(); int t; scanf("%d",&t); while(t--) { scanf("%d %d %d",&n,&m,&k); Top = 0; memset(map,0,sizeof(map)); for(int i=1;i<=k;i++) { int x,y; scanf("%d %d",&x,&y); map[x][y-1] = 1; } pre_dp(); int ans = get_dp(); printf("%d\n",ans); } return 0; } int get_dp() { for(int i=2;i<=n;i++) { for(int j=0;j<=Top-1;j++) { int val = status[j]; bool ch = true; for(int u=0;u<=m-1;u++) { a[u] = val%4; val = val/4; if(a[u]!=0&&(map[i][u]||map[i-1][u])) { ch = false; break; } if(a[u]==2) { if(i<=2||(i>2&&map[i-2][u])) { ch = false; break; } } } if(!ch) { continue; } for(int wb=e[j];wb!=-1;wb=d[wb].next) //优化,提前处理好满足的情况,否则超时 { int u = d[wb].y; dp[i][j] = max(dp[i][j],dp[i-1][u]); } dp[i][j]+=cou[j]; } } int ans = 0; for(int i=0;i<=Top-1;i++) { ans = max(ans,dp[n][i]); } return ans; } void addeage(int x,int y) { d[bian].y = y; d[bian].next = e[x]; e[x] = bian++; } void pre_dp() { int sum = 1; for(int i=1;i<=m;i++) { sum = sum*4; } sum-=1; memset(cou,0,sizeof(cou)); for(int i=0;i<=sum;i++) //求出满足的所有的四进制状态即:三个连续1,两个连续2或3 { int val = i; for(int j=0;j<=m-1;j++) { a[j] = val%4; val = val/4; } bool ch = true; int ds = 0; for(int j=0;j<=m-1;) { if(a[j]!=0) { if(a[j]==1&&(j+2)<=m-1&&a[j+1]==1&&a[j+2]==1) { j+=3; ds++; }else if(a[j]==2&&(j+1)<=m-1&&a[j+1]==2) { j+=2; ds++; }else if(a[j]==3&&(j+1)<=m-1&&a[j+1]==3) { j+=2; }else { ch = false; break; } }else { j++; } } if(ch) { cou[Top] = ds; status[Top++] = i; } } memset(dp,0,sizeof(dp)); memset(e,-1,sizeof(e)); bian = 0; int asum = 0; for(int i=0;i<=Top-1;i++) //优化步骤 { int val = status[i]; for(int j=0;j<=m-1;j++) { a[j] = val%4; val= val/4; } for(int j=0;j<=Top-1;j++) { val = status[j]; bool ch = true; for(int u=0;u<=m-1;u++) { b[u] = val%4; val = val/4; if(a[u]==1&&b[u]==0) { }else if(a[u]==2&&b[u]==3) { }else if(a[u]==3&&b[u]==0) { }else if(a[u]==0&&(b[u]==1||b[u]==2||b[u]==0)) { }else { ch = false; break; } } if(ch) { addeage(i,j); } } } }
POJ 1038 Bugs Integrated, Inc.,布布扣,bubuko.com
POJ 1038 Bugs Integrated, Inc.
标签:des c style class blog code
原文地址:http://blog.csdn.net/yongxingao/article/details/27338619