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 ...
随机推荐
- [翻译] BezierString
BezierString https://github.com/lvnyk/BezierString Rendering NSAttributedStrings along arbitrary con ...
- Linux 系统的/目录
根目录下的重要目录 目录 应放置档案内容 /bin 系统有很多放置执行档的目录,但/bin比较特殊.因为/bin放置的是在单人维护模式下还能够被操作的指令.在/bin底下的指令可以被root与一般帐号 ...
- 评微软收购GitHub
前段时间,微软正式宣布以75亿美元收购GitHub.除了微软和GitHub以外,也许对这笔并购最在意的,正是微软的同城敌友,亚马逊.在我看来,GitHub也许是微软史上最好的收购. 75亿美元贵不贵? ...
- OC中实现可变参数
通过哨兵参数实现,相当于nil. 1.c语言中 #import <stdio.h> #import <stdarg.h> int addemUp(int firstNum,.. ...
- 【从源代码看Android】02MessageQueue的epoll原型
版权声明:本文为博主原创文章,欢迎转载.请注明原文链接 https://blog.csdn.net/ashqal/article/details/31772697 1 开头 上一讲讲到Looper,大 ...
- Day1 Spring初识(一)
在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://www.cnblogs.com/xdp-gacl/p/3707631.html和http://www.cnblogs.com/ ...
- ethers.js-6-Low-Level API
Low-Level API These are advanced, low-level API features that should, for most people not be necessa ...
- 关于VS2010 C#使用DirectX的问题[英]
转载的,就不翻译了…微软把精力放到xna去了.所以推荐大家用XNA,如果非要用托管的DirectX也可以,只不过版本一直是2006年的了. 具体方法: 安装SDK之后 他默认的位置在C:\WINDOW ...
- Usaco2008 Jan
[Usaco2008 Jan] https://www.luogu.org/problemnew/show/P2419 题目描述 N (1 ≤ N ≤ 100) cows, conveniently ...
- 1555: Inversion Sequence (通过逆序数复原序列 vector的骚操作!!!)
1555: Inversion Sequence Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Su ...