题解

42 条题解

  • -1
    @ 2017-08-08 20:38:24

    #include<iostream>
    #include<string.h>
    using namespace std;
    int n,fds[200001],f[100051][20][20];
    void input()
    {
    memset(f,0xc0,sizeof(f));
    f[0][0][0]=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
    char inchr;
    cin>>inchr;
    if(inchr=='M') fds[i]=1;
    else if(inchr=='F') fds[i]=2;
    else fds[i]=3;
    for(int j=0;j<=15;j++)
    {
    for(int k=0;k<=15;k++)
    {
    if(f[i][j][k]>=0)
    {
    if(j==0) f[i+1][fds[i]][k]=max(f[i+1][fds[i]][k],f[i][j][k]+1);
    else if(j/4==0)
    {
    if(j%4==fds[i]) f[i+1][j*4+fds[i]][k]=max(f[i+1][j*4+fds[i]][k],f[i][j][k]+1);
    else f[i+1][j*4+fds[i]][k]=max(f[i+1][j*4+fds[i]][k],f[i][j][k]+2);
    }
    else
    {
    if(j/4==j%4&&j%4==fds[i]) f[i+1][(j%4)*4+fds[i]][k]=max(f[i+1][(j%4)*4+fds[i]][k],f[i][j][k]+1);
    else if(j/4!=j%4&&j/4!=fds[i]&&j%4!=fds[i]) f[i+1][(j%4)*4+fds[i]][k]=max(f[i+1][(j%4)*4+fds[i]][k],f[i][j][k]+3);
    else f[i+1][(j%4)*4+fds[i]][k]=max(f[i+1][(j%4)*4+fds[i]][k],f[i][j][k]+2);
    }
    if(k==0) f[i+1][j][fds[i]]=max(f[i+1][j][fds[i]],f[i][j][k]+1);
    else if(k/4==0)
    {
    if(k%4==fds[i]) f[i+1][j][k*4+fds[i]]=max(f[i+1][j][k*4+fds[i]],f[i][j][k]+1);
    else f[i+1][j][k*4+fds[i]]=max(f[i+1][j][k*4+fds[i]],f[i][j][k]+2);
    }
    else
    {
    if(k/4==k%4&&k%4==fds[i]) f[i+1][j][(k%4)*4+fds[i]]=max(f[i+1][j][(k%4)*4+fds[i]],f[i][j][k]+1);
    else if(k/4!=k%4&&k/4!=fds[i]&&k%4!=fds[i]) f[i+1][j][(k%4)*4+fds[i]]=max(f[i+1][j][(k%4)*4+fds[i]],f[i][j][k]+3);
    else f[i+1][j][(k%4)*4+fds[i]]=max(f[i+1][j][(k%4)*4+fds[i]],f[i][j][k]+2);
    }
    }
    }
    }
    }
    int ans=0;
    for(int i=0;i<=15;i++)
    {
    for(int j=0;j<=15;j++) ans=max(ans,f[n][i][j]);
    }
    cout<<ans<<endl;
    }
    int main()
    {
    input();
    return 0;
    }

  • -1
    @ 2016-11-09 20:21:02
    using namespace std;
    int n,a[100010],ans,dp[2][4][4][4][4],point,pre;char c;
    inline int Get_Char(){  
        char c;  
        do c=getchar(); while(c!='M'&&c!='F'&&c!='B');  
        switch(c){  
            case 'M':return 1;  
            case 'F':return 2;  
            case 'B':return 3;}}  
    inline int score(int x,int y){  
        if(!x)return 1; 
        return x==y?1:2;}  
    inline int score(int x,int y,int z){
        int temp=x*y*z;  
        switch(temp){  
            case 6:return 3;  
            case 1:return 1;
            case 8:return 1;
            case 27:return 1;  
            case 0:return score(y,z);  
            default: return 2;}}
    int main(void){
        scanf("%d",&n);
        memset(dp,0xef,sizeof dp); 
        dp[0][0][0][0][0]=0;
        for(int i=1;i<=n;i++){
            int x=Get_Char();
            memset(dp+(i&1),0xef,sizeof(dp)>>1); 
            point=i&1;
            pre=~i&1;
            for(int i1=0;i1<=3;i1++)                                                                                                                                                                                                 
                for(int i2=(i1?1:0);i2<=3;i2++)
                    for(int i3=0;i3<=3;i3++)
                        for(int i4=(i3?1:0);i4<=3;i4++)if(dp[~i&1][i1][i2][i3][i4]>=0) {
                            int q1=score(i1,i2,x);
                            int q2=score(i3,i4,x);
                            dp[point][i2][x][i3][i4]=max(dp[point][i2][x][i3][i4],dp[pre][i1][i2][i3][i4]+q1);
                            dp[point][i1][i2][i4][x]=max(dp[point][i1][i2][i4][x],dp[pre][i1][i2][i3][i4]+q2);}}
            for(int i1=0;i1<=3;i1++)
                for(int i2=(i1?1:0);i2<=3;i2++)
                    for(int i3=0;i3<=3;i3++)
                        for(int i4=(i3?1:0);i4<=3;i4++)
                            ans=max(ans,dp[n&1][i1][i2][i3][i4]);
        cout<<ans;}
    

信息

ID
1386
难度
5
分类
动态规划 点击显示
标签
递交数
659
已通过
248
通过率
38%
被复制
2
上传者