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 ...
随机推荐
- DEDE 5.7中各函数所在的文件和位置
/include/taglib/tag.lib.php 2 //function GetTags()/include/payment/yeepay.php 415 function log_resul ...
- 37.ROUND() 函数
ROUND() 函数 ROUND 函数用于把数值字段舍入为指定的小数位数. SQL ROUND() 语法 SELECT ROUND(column_name,decimals) FROM table_n ...
- Oracle——分组函数
AVG(平均值)和 SUM (合计)函数 可以对数值型数据使用AVG 和 SUM 函数. AVG组函数忽略空值 --在组函数中使用NVL函数 --求平均值 )) MIN(最小值)和 MAX(最大值)函 ...
- 认识HttpContext.User
HttpContext.User,即IPrincipal .net源代码 namespace System.Security.Principal { /// <summary>Define ...
- APP压力稳定性测试
转自:https://www.cnblogs.com/nuonuozhou/p/8643735.html 1.android系统自带monkey程序,模拟用户触摸屏幕,滑动track ball,按键等 ...
- 转:[web]javascript 增加表單的input
利用javascript增加form的input 這是js的部份 //用來區分不同input的name var element_count = 0; function add_element(obj) ...
- Java Serializable(序列化)的理解和总结
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...
- android和.net webservice中的DES加密算法
也是看了一堆的例子,本身并不会写加密算法,好在只要会用就行了,我们把在app中使用的参数加密,然后在.net端的webservice中进行解密,本身并没有什么问题,但是android下和.net下的d ...
- 一个简单的tcp代理实现
There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...
- NSNotification 消息通知的3种方式
1.Notification Center的概念: 它是一个单例对象,允许当事件发生时通知一些对象,让对象做出相应反应. 它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的. 这 ...