Codeforces1114 D. Flood Fill (DP)(整个区间染成同色)
题意:连续的几个颜色相同的格子称为一个连通块。选一个点为起点,每个操作是把所在连通块变一个颜色,求把整个区间染成同色需要的最少操作数。(注意,每次只能改变所在连通块的颜色,不能任选连通块,除了最开始时)
题解:
对于区间[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)(整个区间染成同色)的更多相关文章
- 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 ...
- CF 1114 D. Flood Fill
D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- [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 ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
随机推荐
- resolve或reject之后还需要return吗
答案: 需要 今日碰到一个问题, 是我的同事发现的,如果不说的话可能一直没有注意到 这个代码 在reject 后还会执行, 但是谁也没有注意到, 但是不会报错, 因为当一个promise是resolv ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- 189. Rotate Array 从右边开始翻转数组
[抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- c语言学习笔记 break语句
比如 for() { for() { break; } } 那个break语句只是跳出它所在的那个for循环,不会把最外面的for循环都跳出去.
- 第一章Python简介
1.Python shell(Python命令解释器) 如下所示 2.Python的交互模式 如下 3.代码编辑器窗口 在上面的那些指令称为源代码. 4.在python中,缩进是有语法意义的. 在某行 ...
- javascript总结5:js常见的数据类型
1 Number 数字类型 :包含正数,负数,小数 十进制表示: var n1 =23; 十六进制表示法:从0-9,a(A)-f(F)表示数字.以0x开头. var n2 = 0x42 2 字符串数据 ...
- jQuery高级
一.动画效果 常用的几种效果都是没有easing参数的,也即动画只能swing.$(selector).animate(styles,speed,easing,callback)中是有easing参数 ...
- log4net工作原理(2)
上回说道:Repository可以说成基于一个log4net配置节创建的log4net容器,它根据log4net配置节的指示创建其他所有对象(Logger/Appender/Filter/Layout ...
- WinForm中的多语言处理
配置文件中存储当前语言环境,切换语言时进行修改,启动程序时读取该配置并设置当前线程的Culture 可根据线程的语言环境动态读取不同的资源文件,不同资源文件名用语言环境文本进行区分
- cinder侧挂载卷流程分析
cinder侧挂载卷流程分析,存储类型以lvm+iscsi的方式为分析基础cinder侧主要调用了三个接口1)reserve_volume: 把volume的状态改为attaching,阻止其它节点执 ...