bzoj 1806: [Ioi2007]Miners 矿工配餐 http://www.lydsy.com/JudgeOnline/problem.php?id=1806
f[2][4][4][4][4]:f[这是一个滚动数组][a][b][c][d] a,b:第一个坑最近两辆车 c,d:第二个坑最近两辆车
如果开到100000的话 64M的内存爆的稳稳的 要开滚动数组
搞memset搞了很久
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[2][4][4][4][4];//f[这是一个滚动数组][a][b][c][d] a,b:第一个坑最近两辆车 c,d:第二个坑最近两辆车
char ch[110000];
int main()
{
int n,now=1,last=0,ans=0;
scanf("%d",&n);
scanf("%s",ch+1);
memset(f[now],0xef,sizeof(f[now]));
f[now][0][0][0][0]=0;
for (int i=1;i<=n;i++)
{
int x;
if (ch[i]=='M') x=1;
if (ch[i]=='F') x=2;
if (ch[i]=='B') x=3;
now=1-now;last=1-last;
memset(f[now],0xef,sizeof(f[now]));
/*
这里要memset成最小值
至于为什么也试了很久(菜)
因为max的时候f[a][b][c][d]这个状态有可能是不存在的(因为这个是靠枚举得到的)
所以如果初始值为0的话max之后有可能会取到这个根本就不存在的方案导致WA掉
其实赋给一个小于-3的值也是ok的(s<=3)
*/
for (int a=0;a<=3;a++)
{
for (int b=0;b<=3;b++)
{
for (int c=0;c<=3;c++)
{
for (int d=0;d<=3;d++)
{
int s=1;
//分配给第一个坑
if (a>0) s++; if (b>0) s++;
if (a==b&&a!=0) s--;
if (x==a) s--;
if (x==b&&a!=b) s--;
f[now][b][x][c][d]=max(f[now][b][x][c][d],f[last][a][b][c][d]+s);
//分配给第二个坑
s=1;
if (c>0) s++; if (d>0) s++;
if (c==d&&c!=0) s--;
if (x==c) s--;
if (x==d&&c!=d) s--;
f[now][a][b][d][x]=max(f[now][a][b][d][x],f[last][a][b][c][d]+s);
}
}
}
}
}
for (int a=0;a<=3;a++)
for (int b=0;b<=3;b++)
for (int c=0;c<=3;c++)
for (int d=0;d<=3;d++)
ans=max(ans,f[now][a][b][c][d]);
printf("%d\n",ans);
return 0;
}