Pku2978 Colored stones

Topic links: the Click here Wallpaper

Solution:

Shaped pressure DP, consider \ (f [i] [j ] [k] \) represents the current can be left to the i-th rock, color state is j, the selected color is the last stone stone maximum number k

Transfer is also very good transfer, enumerate all the states, and then enumerate the last color transferred from the previous state and then transferred violence on the line

Finally, to find what the maximum output n-max in all cases on the line

Code:

#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,f[101][(1<<8)-1][7];
int b[7]={0,1};
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int main(){
    for(int i=2;i<=6;i++) b[i]=b[i-1]*2;
    begin:n=read(),m=read();
    if(n==0&&m==0) return 0;
    memset(f,0,sizeof(f));
    int num=b[m+1]-1,ans=0;
    for(int i=1;i<=n;i++){
        int x=read();
        for(int s=0;s<=num;s++){
            if(!(s&b[x]))
                for(int j=1;j<=m;j++){
                    if(!(s&b[j])) continue;
                    f[i][s|b[x]][x]=max(f[i][s|b[x]][x],f[i-1][s][j]+1);
                    f[i][s][j]=max(f[i][s][j],f[i-1][s][j]);
                }
            else
                for(int j=1;j<=m;j++){
                    if(j==x) f[i][s][x]=max(f[i][s][x],f[i-1][s][j]+1);
                    else if(s&b[j]) f[i][s][j]=max(f[i][s][j],f[i-1][s][j]);
                }
        }
    }
    for(int i=1;i<=m;i++)
        for(int s=0;s<=num;s++)
            if(s&b[i]) ans=max(ans,f[n][s][i]);
    printf("%d\n",n-ans);goto begin;
}

Guess you like

Origin www.cnblogs.com/NLDQY/p/11285670.html