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 ...
随机推荐
- 12.2Data Guard新特性--使用DBMS_DBCOMP.DBCOMP数据比较
Oracle Data Guard会主动对Hot数据(数据正被读取或修改)执行验证, 无论是primary还是standby,但对于那些Cold数据不会做任何检查和校验.所以在12.2版本 ...
- Linux装python3
记住下载的软件最好装在/opt下默认的 大家都这样做 linux装python3.7我们以安装最新的来做测试 先下载关联的包防止出错 安装python前的库环境,非常重要yum install gc ...
- 封装用于解析NSDate的便利的类
封装用于解析NSDate的便利的类 此类可以从NSDate中解析出年份,月份,日期,时,分,秒,毫秒,足够用来做好多事情了,现提供源码如下: 以下是核心的类: TimeInfo.h 与 TimeInf ...
- log4jnet不记录日志的问题解决
背景:dll文件从别人项目里复制过来的,配置文件是从别的项目里配置过来的.然后就是不写日志. 最后经过搜索原项目,发现需要在AssemblyInfo.cs文件里加一段话, [assembly: log ...
- jQuery实现滚动时动态加载页面内容
有些网站的网页内容不是一次性加载完毕的,而是在鼠标向下滚动时动态加载的,这是怎么做到的呢?看下面代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- 使用python来搞定redis的订阅功能
好久没写博客了. 最近公司开了新项目,我负责的内容之一是系统的后端.具体项目内容我就不介绍了,但是用到的技术有些还是很有趣的,值得记录一下.今天介绍的就是其中一个:利用redis的pubsub订阅 ...
- MySQL管理.md
用户管理 创建 举例 mysql> create user test@localhost identified by 'password'; Query OK, 0 rows affected ...
- [SCOI2012]奇怪的游戏
题目 话说有没有跟我一样直接猜了一个最大值不会改变这样一个二乎乎的结论之后交上去保龄的呀 首先看到棋盘,选择相邻的格子,非常经典的黑白染色 显然那个二乎乎的结论是错的,随便就能\(hack\)了 于是 ...
- bootstrap 多级下拉菜单
如上效果: 实现代码: 导入js和css: <link rel="stylesheet" href="http://cdn.static.runoob.com/li ...
- python 小技巧同时迭代多个列表,多变量同时运算
再来一个大家很熟悉的 dict={"a": "b","c": "d"}k,v=dict.items()print(k,v ...