CF 1114 D. Flood Fill
D. Flood Fill
题意:
一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次。n<=5000
分析:
区间dp。
dp[i][j][0/1]表示当前的区间是l~r,把这一段变成一个颜色的最少次数,最后一个改变的颜色是最左边/最右边的一段。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int f[N][N][], a[N], b[N]; int dp(int l,int r,int p) {
if (l > r) return ;
if (l == r) return ;
if (f[l][r][p]) return f[l][r][p];
int &res = f[l][r][p];
if (p == ) {
res = dp(l + , r, ) + (a[l + ] != a[l]);
res = min(res, dp(l + , r, ) + (a[r] != a[l]));
}
else {
res = dp(l, r - , ) + (a[l] != a[r]);
res = min(res, dp(l, r - , ) + (a[r - ] != a[r]));
}
return res;
}
int main() {
int n = read();
for (int i = ; i <= n; ++i) a[i] = read();
int cnt = ;
for (int i = ; i <= n; ++i) if (a[i] != a[i - ]) b[++cnt] = a[i];
n = cnt;
for (int i = ; i <= n; ++i) a[i] = b[i];
cout << min(dp(, n, ), dp(, n, ));
return ;
}
CF 1114 D. Flood Fill的更多相关文章
- CF 538 D. Flood Fill 递归 区间DP
link:https://codeforces.com/contest/1114/problem/D 题意: 给定一个数组,有不同的颜色,你可以从任意一个位置开始,改变颜色,相邻的是同一种颜色的位子的 ...
- 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 ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [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 ...
随机推荐
- webservice双向验证
ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtoco ...
- [翻译] AFSoundManager
AFSoundManager iOS audio playing (both local and streaming) and recording made easy through a comple ...
- November 8th 2016 Week 46th Tuesday
When he can't, he tries a new way to share a new pair. 当他做不到时,他尝试一种新的方式:分享. To share, your failing e ...
- jquery与json的结合
通过AJAX异步减少网络内容传输,而JSON则可以把传输内容缩减到纯数据:然后利用jQuery内置的AJAX功能直接获得JSON格式的数据:在客户端直接绑定到数据控件里面,从而达到最优. 1 2 3 ...
- impala jdbc驱动执行impala sql的一个坑(不支持多行sql)
架构使用spark streaming 消费kafka的数据,并通过impala来插入到kudu中,但是通过对比发现落地到kudu表中的数据比kafka消息数要少,通过后台日志发现,偶发性的出现jav ...
- 2002. [HNOI2010]弹飞绵羊【LCT】
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- 【转】Android应用如何跳转到应用市场详情页面
Android应用开发过程中,可能会有需求,比如:推广时跳转到应用市场下载应用,跳转到应用市场给自己的应用打分,跳转到应用市场更新自己的应用.那如何跳转到应用市场呢? 可能跳转的方法大家都是知道的,方 ...
- sed 以及 awk用法
sed 格式 sed[options] "script" FILE.... 选项: -n:静默模式,不输出模式空间内的内容:默认打印空间模式的内容 -r:扩展的正则表达式 -f 文 ...
- 多启动引导工具——AIO Boot
该软件功能十分强大 官网介绍的也十分详尽 这里仅仅简单标记一下用来以后查找 https://www.aioboot.com/en/ 效果图: 支持多语言: 简单运用:
- python range函数与numpy arange函数,xrange与range的区别
转自:https://www.cnblogs.com/ymjyqsx/p/6426764.html 1.range()返回的是range object,而np.arange()返回的是numpy.nd ...