Codeforces 1114D Flood Fill

题目链接:https://www.luogu.org/problem/CF1114D

题意:n 个方块排成一排,第 i 个颜色为 c_i。定义一个颜色联通块 [l,r]当且仅当 l 和 r之间(包括 l,r)所有方块的颜色相同。现在你可以选定一个起始位置 p,每次将 p 所在颜色联通块的所有方块颜色改成另一种。这个操作可能将多个颜色联通块合并成一个。问最少要多少步,能让 [1,n] 变成一个颜色联通块。

分析:区间DP这点很容易看出来

预处理把初始时相同颜色的变成一块也容易想

我在区间dp的时候想的是DP数组用一个结构体,存val值和这一块的颜色(其实这已经不满足无后效性了)

对于两个相同颜色中间夹的情况不满足

#include<bits/stdc++.h>
using namespace std;
int n, x, a[5010], f[5010][5010], y, cnt;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) {
        scanf("%d", &x);
        if (x != y) a[++ cnt] = x;
        y = x;
    }
    for (int i = 1; i <= cnt; i ++)
        for (int j = 1; j + i <= cnt; j ++)
            if (a[j] == a[j + i])
                f[j][j + i] = f[j + 1][j + i - 1] + 1;
            else f[j][j + i] = std::min(f[j + 1][j + i], f[j][j + i - 1]) + 1;
    return printf("%d\n", f[1][cnt]), 0;
}

猜你喜欢

转载自www.cnblogs.com/qingjiuling/p/11627068.html