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以内, ...
随机推荐
- ORACLE常用语句:
ORACLE常用语句: 1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新 ...
- 响应式Web设计与CSS(下)
4.媒体类型与媒体查询 4.1 媒体类型 依据设备能力来分离样式的能力,始于媒体类型. 媒体类型用于针对特定的环境应用样式,包括屏幕显示.打印和电视等. 通过给link元素添加media属性,可以指定 ...
- 微信小程序之回到顶部的两种方式
第一种:使用view标签形式回到顶部 WXML: <image src='../../img/button-top.png' class='goTop' hidden='{{!floorstat ...
- Cenos系统防火墙开放指定端口
1.查看已经开放的端口 firewall-cmd --list-ports 2.开启指定端口 firewall-cmd --zone=public --add-port=2181/tcp --perm ...
- C++游戏(大型PC端枪战游戏)服务器架构
实习期间深入参与到某大型pc端枪战游戏的后端开发中,此游戏由著名游戏工作室编写,代码可读性极高,自由时间对游戏后台代码进行了深入研究,在满足自身工作需要的同时对游戏后台的架构也有了理解,记录在此,以便 ...
- SourceTreet提交时显示remote: Incorrect username or password ( access token )(4种解决办法)
引言 我因为第一次安装Sources Tree的时候进行破解时(跳过安装时的登录),因为操作失误造成了好多bug,导致Sources Tree不论提交,拉取,获取,都会报remote: Incorre ...
- noip复习——线性筛(欧拉筛)
整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...
- Python 函数为什么会默认返回 None?
Python 有一项默认的做法,很多编程语言都没有--它的所有函数都会有一个返回值,不管你有没有写 return 语句. 本文出自"Python为什么"系列,在正式开始之前,我们就 ...
- 01@-tornado
import tornado.web ''' tornado的基础web框架模块 ''' import tornado.ioloop ''' tornado的核心IO循环模块 封装了Linux的epo ...
- JDK1.8源码学习-String
JDK1.8源码学习-String 目录 一.String简介 String类是Java中最常用的类之一,所有字符串的字面量都是String类的实例,字符串是常量,在定义之后不能被改变. 二.定义 p ...