BZOJ 1806 矿工配餐(DP)
很水的DP。
因为每一个餐车的加入只需要知道当前矿洞的前两个餐车种类就行了。而餐车一共就三种。
所以令dp[i][Sa][Sb]表示前i辆餐车送餐完毕后第一个矿洞的前两个餐车种类为Sa,第二个矿洞的前两个餐车种类为Sb的最大产矿量。
滚动数组优化一下,然后搞一搞就行了。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 45989 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=100005; //Code begin... int dp[2][16][16], a[N], vis[4]; char s[N]; int check(int x, int y, int z){ mem(vis,0); vis[x]=vis[y]=vis[z]=1; int res=0; FOR(i,1,3) if (vis[i]) ++res; return res; } int main () { int n, flag=0; scanf("%d%s",&n,s+1); FOR(i,1,n) { if (s[i]=='M') a[i]=1; else if (s[i]=='B') a[i]=2; else a[i]=3; } mem(dp,-1); dp[0][0][0]=0; FOR(i,1,n) { flag^=1; mem(dp[flag],-1); FOR(j,0,3) FOR(k,0,3) FOR(l1,0,3) FOR(l2,0,3) { if (dp[flag^1][j*4+k][l1*4+l2]==-1) continue; dp[flag][a[i]*4+j][l1*4+l2]=max(dp[flag][a[i]*4+j][l1*4+l2],dp[flag^1][j*4+k][l1*4+l2]+check(a[i],j,k)); dp[flag][j*4+k][a[i]*4+l1]=max(dp[flag][j*4+k][a[i]*4+l1],dp[flag^1][j*4+k][l1*4+l2]+check(a[i],l1,l2)); } } int ans=0; FOR(j,0,3) FOR(k,0,3) FOR(l1,0,3) FOR(l2,0,3) ans=max(ans,dp[flag][j*4+k][l1*4+l2]); printf("%d\n",ans); return 0; }