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

题解:

对于区间[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. swfupload上传文件数量限制之setStats()

    使用swfupload仿赶集的图片上传 SWFUpload是一个基于flash与javascript的客户端文件上传组件. handlers.js文件 完成文件入列队(fileQueued) → 完成 ...

  2. IWebBrowser2不能复制剪切

    项目中嵌入了IE控件,近期做了一次大改版,发现网页不能进行复制和剪切了,折腾了半天,发现是com初始化有问题: 修正前的方式: CoInitialize(NULL); // do your work ...

  3. 使用递归函数,输出n个元素的所有子集

    题目描述: 请编写一个递归函数,用来输出n个元素的所有子集.例如,三个元素{a,b,c}的所有子集是:{},{a},{b},{c},{a,c},{ac},{b,c},{a,b,c}. 解题思路: 根据 ...

  4. 1-如何自己在eclipse上配置Andriod环境

    转载:http://blog.csdn.net/dr_neo/article/details/49870587 最新鲜最详细的Android SDK下载安装及配置教程 2015年11月16日 19:2 ...

  5. 在C语言中如何嵌入python脚本

    最近在写配置文件时,需要使用python脚本,但脚本是一个监控作用,需要它一直驻留在linux中运行,想起C语言中能够使用deamon函数来保留一个程序一直运行,于是想到写一个deamon,并在其中嵌 ...

  6. jQuery基础教程-第8章-002Adding jQuery object methods

    一.Object method context 1.We have seen that adding global functions requires extending the jQuery ob ...

  7. Linux cmus

    一.简介 CMus 是一款类似于MOC, Herrie 或 mp3blaster 的基于终端的音乐播放器,支持 Ogg Vorbis, FLAC, MP3, WAV, Musepack, WavPac ...

  8. springboot-条件化注解

    在项目中,有时会遇到我们的Configuration.Bean.Service等等的bean组件需要依条件按需加载的情况.那么Spring Boot怎么做的呢?它为此定义了许多有趣的条件,当我们将它们 ...

  9. HackTwelve 为背景添加圆角边框

    1.概要:     ShapeDrawable是一个为UI控件添加特效的好工具.这个技巧适用于那些可以添加背景的控件 2.添加圆角边框其实就是添加的背景那里不是直接添加图片,而是添加一个XML文件即可 ...

  10. Android Activity的切换动画(overridePendingTransition)

    overridePendingTransition 1.平时Activity的切换是就是从中间弹出来,然后遮盖住之前的Activity.这种效果看到很多后就想给他换成其他的效果,如: 要显示的Acit ...