CodeForces - 1114D-Flood Fill (区间dp)
You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ii-th square initially has the color cici.
Let's say, that two squares ii and jj belong to the same connected component if ci=cjci=cj, and ci=ckci=ck for all kk satisfying i<k<ji<k<j. In other words, all squares on the segment from ii to jj should have the same color.
For example, the line [3,3,3][3,3,3] has 11 connected component, while the line [5,2,4,4][5,2,4,4] has 33 connected components.
The game "flood fill" is played on the given line as follows:
- At the start of the game you pick any starting square (this is not counted as a turn).
- Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
Input
The first line contains a single integer nn (1≤n≤50001≤n≤5000) — the number of squares.
The second line contains integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤50001≤ci≤5000) — the initial colors of the squares.
Output
Print a single integer — the minimum number of the turns needed.
Examples
4
5 2 2 1
2
8
4 5 2 2 1 3 5 5
4
1
4
0
能用区间dp一个很重要的原因是只能通过一个起点更新
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn=5e3+;
typedef long long ll;
using namespace std;
vector<int>vec;
int dp[][]; int main()
{
int n;
cin>>n;
int x;
for(int t=;t<=n;t++)
{
scanf("%d",&x);
vec.push_back(x);
}
vec.erase(unique(vec.begin(),vec.end()),vec.end());
int nn=vec.size();
memset(dp,0x3f3f3f3f,sizeof(dp));
for(int t=;t<nn;t++)
{
dp[t][t]=;
}
for(int t=nn-;t>=;t--)
{
for(int len=;t+len<nn;len++)
{
if(vec[t]==vec[t+len])
{
if(len==)
{
dp[t][t+len]=;
}
else
{
dp[t][t+len]=dp[t+][t+len-]+;
}
}
else
{
dp[t][t+len]=min(dp[t][t+len-],dp[t+][t+len])+;
}
}
}
cout<<dp[][nn-]<<endl;
//system("pause");
return ;
}
CodeForces - 1114D-Flood Fill (区间dp)的更多相关文章
- Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)
题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...
- D. Flood Fill 区间DP 或lcs匹配
题意 给定一串数字 相同的连续的数字可以同时 转换成一个相同数字 问最小几次可以全部转换成一个相同的数字 法1:区间dp dp[l][r][0/1] 0表示l r区间转化成和最左边相同需要多少次 ...
- codeforces1114D. Flood Fill(区间Dp)
传送门: 解题思路: 区间Dp,发现某一个区间修改后区间颜色一定为左边或右边的颜色. 那么只需要设方程$f_(l,r,0/1)$表示区间$[l,r]$染成左/右颜色的最小代价 转移就是枚举左右颜色就好 ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
- CF1114D Flood Fill(DP)
题目链接:CF原网 题目大意:$n$ 个方块排成一排,第 $i$ 个颜色为 $c_i$.定义一个颜色联通块 $[l,r]$ 当且仅当 $l$ 和 $r$ 之间(包括 $l,r$)所有方块的颜色相同.现 ...
- Codeforces.392E.Deleting Substrings(区间DP)
题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...
- Codeforces 983B. XOR-pyramid【区间DP】
LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...
- CodeForces - 1025D: Recovering BST (区间DP)
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...
- Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...
随机推荐
- python4.2参数传入
#顺序传入参数def show(name,age,sex,hobby):#形参 print("我叫:",name,"年龄:",age,"性别:&quo ...
- Linux中C++提示‘close’ was not declared
C++socket编程时关闭socket和epoll时出现‘close’ was not declared,是程序头文件缺失导致的.缺失头文件#include <unistd.h>
- C#LeetCode刷题之#119-杨辉三角 II(Pascal‘s Triangle II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3690 访问. 给定一个非负索引 k,其中 k ≤ 33,返回杨辉 ...
- Linux内核之 进程管理
正如上一篇我们提到过,进程是Linux系统中仅次于文件的基本抽象概念.正在运行的进程不仅仅是二进制代码,而是数据.资源.状态和虚拟的计算机组成.我们今天主要介绍进程的概念,组成,运行状态和生命周期等. ...
- JavaFX桌面应用-视频转码工具(支持爱奇艺qsv转mp4)
最近由于需要将在爱奇艺下载的视频(qsv)转化了mp4,用JavaFX开发一个视频转码工具,算是JavaFX开发的第一个应用吧. 支持qsv转码mp4,理论上支持各种格式,仅测试了flv,qsv格式. ...
- WKWebView 网络请求Header 丢失
WKWebView 是苹果手机上主要的H5加载控件,它相比UIWebView 有诸多优势.在次不做比较,但是它的坑缺比较多.网上也有很多的例子但是做的比较好的真不多,我在这里推荐俩博客供大家参考.ht ...
- Nginx同时支持Http和Https
现在的网站支持Https几乎是标配功能,Nginx能很好的支持Https功能.下面列举一个配置同时支持Http和Https的功能. 需要注意的是:既然选择使用Https,就是为了保证通信安全,那么就没 ...
- python:**kwargs
**kwargs接收键值对参数,即字典, dict的pop()函数内需传2个参数,第一个参数为dict内的key, 如果有该key>第二个参数为None,最后的结果就是该key对应的value. ...
- yb课堂之压力测试工具Jmeter5.X 实战《二十二》
目前常用的测试工具对比 LoadRunner 性能稳定,压测结果及细粒度大,可以自定义脚本进行压力,但是太过于重大,功能比较繁多 Apache AB(单接口压测最方便) 模拟多线程并发请求,ab命令对 ...
- PythonCrashCourse 第十章习题
在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以"In Python you can"打头.将这个文件命名为 learning_pytho ...