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

题解:

对于区间[L,R],最优的方案要么是全变成L处的颜色,要么全变成R处的颜色

因为可以看作是先选一个格子为起点,然后不断地将当前所在联通块与相邻格子合并,合并后一定是相邻格子的颜色才最优

那么,设f(i,j,0/1)表示区间[i,j]变为i/j处的颜色的最少操作次数

f(i,j,0)由f(i+1,j,0/1)转移来,f(i,j,1)由f(i,j-1,0/1)转移来,转移附加个颜色是否相同就行了,见代码。

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
int a[],dp[][][];
int main()
{
int n;
scanf("%d",&n);
int cnt=;
for(int i= ; i<=n ; i++)
{
int x; scanf("%d",&x);
if(x!=a[cnt])
{
a[++cnt]=x;
}
}
n=cnt;
for(int k= ; k<=n ; k++)
{
for(int i= ; i<=n ; i++)
{
int j=i+k-;
if(j>n) break;
dp[i][j][] = min(dp[i+][j][]+(a[i]!=a[i+]) , dp[i+][j][]+(a[i]!=a[j]));
dp[i][j][] = min(dp[i][j-][]+(a[i]!=a[j]) , dp[i][j-][]+(a[j-]!=a[j]));
}
}
printf("%d\n",min(dp[][n][],dp[][n][]));
}

滚动数组

#include<bits/stdc++.h>
using namespace std;
const int N=;
int n,f[N][N],bf[N],cnt,ans,pre[N][],fore[N][]; int main()
{
// freopen("in.in","r",stdin);
// freopen("2.out","w",stdout);
scanf("%d",&n);
cnt++;
scanf("%d",&bf[cnt]);
for(int i=;i<=n;i++)
{
int tem;
scanf("%d",&tem);
if(tem!=bf[cnt])
{
cnt++;
bf[cnt]=tem;
}
} for(int k=;k<=cnt;k++)
{
for(int i=;i<=cnt-k+;i++)
{
f[i][k]=max(fore[i+k-][(k-)%],max(pre[i+][(k-)%],f[i][k]));
if(bf[i]==bf[i+k-])f[i][k]++;
pre[i][k%]=max(pre[i][(k-)%],f[i][k]);
fore[i+k-][k%]=max(fore[i+k-][(k-)%],f[i][k]);
}
}
int ans=max(pre[][cnt%],fore[cnt][cnt%]);
ans=cnt--ans;
printf("%d\n",ans);
return ;
}

还有一种方法是:先把初始颜色序列去重,设去重后长度为n,然后找最长回文子序列len,答案就是n-ceil(len/2)。

因为对于一个回文子序列,只需操作floor(len/2)次,非回文序列长度为n必须两两合并共n-1次,因为起点可以任选,所以选最长回文子序列的中点作为起点,共操作n-ceil(len/2)次。

---------------------
原文:https://blog.csdn.net/Wen_Yongqi/article/details/86989782

Codeforces1114 D. Flood Fill (DP)(整个区间染成同色)的更多相关文章

  1. 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 ...

  2. CF 1114 D. Flood Fill

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

  3. [LeetCode] Flood Fill 洪水填充

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

  4. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  5. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  6. [Swift]LeetCode733. 图像渲染 | Flood Fill

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

  7. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

  8. [LeetCode&Python] Problem 733. Flood Fill

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

  9. LeetCode - Flood Fill

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

随机推荐

  1. 数字图像处理实验(15):PROJECT 06-02,Pseudo-Color Image Processing 标签: 图像处理MATLAB 2017-05-27 20:53

    实验要求: 上面的实验要求中Objective(实验目的)部分是错误的. 然而在我拿到的大纲中就是这么写的,所以请忽视那部分,其余部分是没有问题的. 本实验是使用伪彩色强调突出我们感兴趣的灰度范围,在 ...

  2. R 如何 隐藏坐标轴

    x = c(7,5,8)dim(x)<-3names(x)<-c("apple","banana", "cherry")plot ...

  3. 专题2-通过按键玩中断\第1课-中断处理流程深度剖析-lesson1

    中断概念 1.中断生命周期 串口先产生一个事件,该事件传送到中断控制器里面,中断控制器会进行相应过滤,能通过过滤,那么就交给CPU去处理. 2.中断源 2440芯片手册 6410芯片手册 3.中断过滤 ...

  4. CopyOnWriteArrayList原理

    http://blog.csdn.net/chayangdz/article/details/76347465 总结的很到位: http://www.cnblogs.com/java-zhao/p/5 ...

  5. 使用HttpServletRequestWrapper修改请求参数 和 使用HttpServletResponseWrapper截获响应数据

    Servlet规范中的Filter引入了一个功能强大的拦截模式.Filter能在request到达servlet的服务方法之前拦截request对象,而在服务方法转移控制后又能拦截response对象 ...

  6. 丢弃昂贵的Detours Professional 3.0,使用免费强大的EasyHook

    我们要先看看微软官方的著名HOOK库: Detours Professional 3.0 售价:US$9,999.95 功能列表: Detours 3.0 includes the following ...

  7. Claims Based Authentication and Token Based Authentication和WIF

    基于声明的认证方式,其最大特性是可传递(一方面是由授信的Issuer,即claims持有方,发送到你的应用上,注意信任是单向的.例如QQ集成登录,登录成功后,QQ会向你的应用发送claims.另一方面 ...

  8. 为docker设置国内镜像

    docker的默认镜像(https://hub.docker.com/)地址,拉取镜像时是比较慢的,经常会超时,有时拉取几个小时.为了加快拉取的时间和速度,需要添加中国的镜像地址: 国内的加速地址: ...

  9. python学习手册 (第3版)

    第一部分 使用入门 第二部分 类型和运算 第三部分 语句和语法 第四部分 函数 第五部分 模块 第六部分 类和OOP 第七部分 异常和工具 第1章 问答环节 人们为何使用Python:可读性.一致性和 ...

  10. js作用域解析原理

    当代码进入到<script>标签或者在调用一个方法,那么就会进入作用域,在解析代码的时候就会做以下两件事情: ①去找var 和function关键字进行js预解析如果有var把值全部定义成 ...