首页 > 其他分享 >Codeforces Beta Round 18 (Div. 2 Only) E

Codeforces Beta Round 18 (Div. 2 Only) E

时间:2023-12-04 21:35:30浏览次数:44  
标签:26 int 18 mn Codeforces Only dp

111
感觉写的好多都是2000分 dp + 路径
这个dp 很明显发现只和 行相关 然后我们发现每行最多俩个 那么肯定就是ababab这种交叉
dp i a b 就是我们第i行选了 a b 交叉的min
转移也是26*26 预处理 cost i a b 作为每行的转移代价即可
最后要注意就是m==1的情况 然后初始化一定要把所有dp [0] 全初始为0

int dp[510][26][26],cost[510][26][26];
void solve(){
    memset(dp,0x3f3f,sizeof dp);
    int n,m;cin>>n>>m;
    int c[n+1][m+1];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char x;cin>>x;
            c[i][j]=x-'a';
        }
        for(int a=0;a<=25;a++){
            for(int b=0;b<=25;b++){
                int w=0;
                for(int j=1;j<=m;j++){
                    if(j&1)w+=(c[i][j]!=a);
                    else w+=(c[i][j]!=b);
                }
                cost[i][a][b]=w;
            }
        }
    }
    for(int i=0;i<=25;i++){
        for(int j=0;j<=25;j++){
            dp[0][i][j]=0;
        }
    }
    int mn=INF,x,y;
    for(int i=1;i<=n;i++){
        for(int a=0;a<=25;a++){
            for(int b=0;b<=25;b++){
                if(a==b)continue;
                for(int d=0;d<=25;d++){
                    for(int e=0;e<=25;e++){
                        if(d==e)continue;
                        if(m!=1)if(a==d||b==e)continue;
                        if(m==1)if(a==d)continue;
                        dp[i][a][b]=min(dp[i][a][b],dp[i-1][d][e]+cost[i][a][b]);
                        if(i==n&&mn>dp[i][a][b]){
                            mn=min(mn,dp[i][a][b]);
                            x=a,y=b;
                        }
                    }
                }
            }
        }
    }
    cout<<mn<<endl;
    vector<PII>ans(n+1);
    ans[n]={x,y};
    for(int i=n-1;i>=0;i--){
        for(int a=0;a<=25;a++){
            for(int b=0;b<=25;b++){
                if(a==b||a==x||b==y)continue;
                if(dp[i][a][b]==dp[i+1][x][y]-cost[i+1][x][y]){
                    ans[i]={a,b};
                    x=a,y=b;
                    goto out;
                }
            }
        }
        out:1;
    }
    for(int i=1;i<=n;i++){
        auto [x,y]=ans[i];
        for(int j=1;j<=m;j++){
            if(j&1)cout<<(char)(x+'a');
            else cout<<(char)(y+'a');
        }cout<<endl;
    }
}

标签:26,int,18,mn,Codeforces,Only,dp
From: https://www.cnblogs.com/ycllz/p/17876038.html

相关文章

  • CF1833G Ksyusha and Chinchilla 题解
    题意:思路:当$n\not\equiv0\space(mod\space3)$时,无解;当$n\equiv0\space(mod\space3)$时,设$size_u$表示以$u$为根的子树还剩余的节点个数,自根节点向叶子节点递归,返回时进行处理节点$u$:设节点$u$的子节点为长度为$len$的序列$v$,设......
  • [Educational Codeforces Round 159 (Rated for Div. 2)](https://codeforces.com/con
    EducationalCodeforcesRound159(RatedforDiv.2)好困,差点没打A-BinaryImbalance#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;voidsolve(){ strings; intn; cin>>n; cin>>s; if(n==......
  • Codeforces Round 800 (Div. 2)
    CodeforcesRound800(Div.2)基本情况A题秒了。B题写了个递推,但是T了,这种构造题还是得多练。B.ParanoidString我的解法#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>usingll=longlong;constintN=2e5+10;intt,n;char......
  • Codeforces Beta Round 10 C
    111发现d(x)只有0-9的值我们可以把按d(x)来分类发现要只计算我们就可以枚举d(C)d(a)d(b)贡献就是这三个任选要是有前面限制呢我打了一下表发现要是AB=C那么肯定满足后面这样我们就只用枚举C然后算他有多少对因子即可然后发现C是1-n连续的可以直接枚举因子A就可以快速算出有......
  • Codeforces Round 912 (Div. 2) - sol
    CodeforcesRound912(Div.2)-solCodeforcesRound912(Div.2)一直是因为晚上打太晚了就没有打过cf,所以只能vp了。/kk四道题有关位运算——不好评价。A.HalloumiBoxes给出\(n\)个数\(a_1,\dots,a_n\),和一个数\(k\)。问是否能通过任意次翻转\(\lek\)的连......
  • Codeforces Round 910 (Div. 2)
    CodeforcesRound910(Div.2)A.MilicaandStringwa麻了,,,不知道自己在干什么#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;voidsolve(){intk,n;strings;cin>>n>>k>>s;in......
  • Codeforces Round 909 (Div. 3)
    CodeforcesRound909(Div.3)A#include<bits/stdc++.h>#defineintlonglong#defineendl'\n';usingnamespacestd;intn;voidsolve(){cin>>n;for(inti=1;i<=10;i++){if(i&1){if(n%3==0)n++;......
  • Codeforces Round 911 (Div. 2)
    CodeforcesRound911(Div.2)A.CoverinWater,,,mc无限水#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;voidsolve(){intn;strings;cin>>n>>s;s=""+s+......
  • Codeforces Round 912 (Div. 2)
    CodeforcesRound912(Div.2)什么位运算专场A.HalloumiBoxes#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;inta[110];intn,k;voidsolve(){cin>>n>>k;for(inti=0;i<n;i++)cin&......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    EducationalCodeforcesRound158(RatedforDiv.2)AEDU的题总是感觉写起来怪怪的#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;inta[101];voidsolve(){intn,x;cin>>n>>x;intans=0;......