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 ...
随机推荐
- SpringBoot13 利用mybatis-plus自动生成entity、dao、service、controller
1 环境配置 = 2 新建一个新的springboot项目 2.1 选择一些必要的依赖 web jpa mysql <?xml version="1.0" encoding= ...
- virsh 查看hypervisor特性
[root@opennebula var]# virsh -c qemu:///system nodeinfo CPU model: x86_64 CPU(s): CPU frequency: MHz ...
- OpenCV---resize
转自http://www.cnblogs.com/korbin/p/5612427.html 在图像处理过程中,有时需要把图像调整到同样大小,便于处理,这时需要用到图像resize() 原函数void ...
- .Net插入大批量数据
1. 使用SqlDataAdapter /// <summary> /// 实现数据库事务,大批量新增数据 /// </summary> ...
- 编写高质量代码改善C#程序的157个建议——建议33:避免在泛型类型中声明静态成员
建议33:避免在泛型类型中声明静态成员 在上一建议中,已经理解了应该将MyList<int>和MyList<string>视作两个完全不同的类型,所以,不应该将MyList&l ...
- SQL之TCL
TCL(Transaction Control Language)事务控制语言 COMMIT 提交SAVEPOINT 设置保存点ROLLBACK 回滚SET TRANSACTION
- HTML5移动开发即学即用(双色) 王志刚 pdf扫描版
HTML5已经广泛应用于各智能移动终端设备上,而且绝大部分技术已经被各种最新版本的测览器所支持:逐一剖析HTML5标准中包含的最新技术,详细介绍了HTML5新标准中提供的各种API,各种各样的应用实例 ...
- Graphics 小记
1.切图 drowg.DrawImage(productImg1, new System.Drawing.Rectangle(30, 30, 300, 300), new System.Drawing ...
- iOS APP打包上传到APPstore的最新步骤
一.前言: 作为一名iOS开发者,把辛辛苦苦开发出来的App上传到App Store是件必要的事.但是很多人还是不知道该怎么上传到App Store上 下面就来详细讲解一下具体流程步骤. 二.准备: ...
- 模态显示PresentModalViewController
1.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...