传送门:

解题思路:

区间Dp,发现某一个区间修改后区间颜色一定为左边或右边的颜色。

那么只需要设方程$f_(l,r,0/1)$表示区间$[l,r]$染成左/右颜色的最小代价

转移就是枚举左右颜色就好了,时间复杂度$O(n^2)$

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define L 0
#define R 1
int n,n_;
int c[];
int f[][][];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
n_++;
scanf("%d",&c[n_]);
if(c[n_]==c[n_-])n_--;
}
n=n_;
for(int len=,l;len<=n;len++)for(l=;l<=n-len+;l++)
{
int r=l+len-;
f[l][r][L]=std::min(f[l+][r][L]+(c[l]!=c[l+]),f[l+][r][R]+(c[r]!=c[l]));
f[l][r][R]=std::min(f[l][r-][L]+(c[l]!=c[r]),f[l][r-][R]+(c[r]!=c[r-]));
}
printf("%d\n",std::min(f[][n][L],f[][n][R]));
return ;
}

codeforces1114D. Flood Fill(区间Dp)的更多相关文章

  1. D. Flood Fill 区间DP 或lcs匹配

    题意 给定一串数字 相同的连续的数字可以同时 转换成一个相同数字 问最小几次可以全部转换成一个相同的数字 法1:区间dp  dp[l][r][0/1]  0表示l r区间转化成和最左边相同需要多少次 ...

  2. Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)

    题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...

  3. CodeForces - 1114D-Flood Fill (区间dp)

    You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The  ...

  4. CF1114D Flood Fill(DP)

    题目链接:CF原网 题目大意:$n$ 个方块排成一排,第 $i$ 个颜色为 $c_i$.定义一个颜色联通块 $[l,r]$ 当且仅当 $l$ 和 $r$ 之间(包括 $l,r$)所有方块的颜色相同.现 ...

  5. Codeforces1114 D. Flood Fill (DP)(整个区间染成同色)

    题意:连续的几个颜色相同的格子称为一个连通块.选一个点为起点,每个操作是把所在连通块变一个颜色,求把整个区间染成同色需要的最少操作数.(注意,每次只能改变所在连通块的颜色,不能任选连通块,除了最开始时 ...

  6. Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

    任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...

  7. CF 1114 D. Flood Fill

    D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...

  8. poj1651(区间dp)

    题目连接:http://poj.org/problem?id=1651 题意:给出一组N个数,每次从中抽出一个数(第一和最后一个不能抽),该次的得分即为抽出的数与相邻两个数的乘积.直到只剩下首尾两个数 ...

  9. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

随机推荐

  1. Bootstrap组件之输入框组

    .input-group--设置div为输入框组: .input-group-lg..input-group-sm..input-group-xs--改变输入框组的尺寸: .input-group-a ...

  2. 【递推DP】POJ1163The Triangle

    题目链接:http://poj.org/problem?id=1163 事实上这个题目有非常多解法,可是我们能够看下这个用一位数组的高效动规解法,这个我上课时老师讲的,非常不错. 先保存最后一行4 5 ...

  3. mysql-组合查询

    一.组合查询 mysql允许执行多个查询(多条select语句),并将结果作为单个查询结果集返回.这些组合查询通常称为并(union)或复合查询(compound query). 有两种情况需要使用组 ...

  4. Android面试精华

    SIM卡的EF文件有什么作用? SIM卡里的全部文件按树来组织: 主文件MF(Master File)--主文件仅仅有文件头,里面存放着整个SIM卡的控制和管理信息 专用文件DF(Dedicated ...

  5. hdu_5139 概率问题

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; int main( ...

  6. 福建省赛-- Common Tangents(数学几何)

    Problem B Common Tangents Accept: 191    Submit: 608 Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  7. 8.最佳的MongoDB客户端管理工具

    转自:https://blog.csdn.net/chszs/article/details/51348248

  8. [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)

    题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...

  9. RHEL启动错误:Kernel panic - not syncing:Attempted to kill init!解决方案

    Virtual Box虚拟机启动RHEL系统报错,错误信息如下: 解决方案: 在GRUB引导界面按下e键,进入下图所示界面. 选择第二项,按下e键,进入编辑状态 在结尾追加enforcing=0,按下 ...

  10. 200 from memory cache / from disk cache / 304 Not Modified 区别

    三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...