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

Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
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)的更多相关文章

  1. Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)

    题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...

  2. D. Flood Fill 区间DP 或lcs匹配

    题意 给定一串数字 相同的连续的数字可以同时 转换成一个相同数字 问最小几次可以全部转换成一个相同的数字 法1:区间dp  dp[l][r][0/1]  0表示l r区间转化成和最左边相同需要多少次 ...

  3. codeforces1114D. Flood Fill(区间Dp)

    传送门: 解题思路: 区间Dp,发现某一个区间修改后区间颜色一定为左边或右边的颜色. 那么只需要设方程$f_(l,r,0/1)$表示区间$[l,r]$染成左/右颜色的最小代价 转移就是枚举左右颜色就好 ...

  4. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

  5. CF1114D Flood Fill(DP)

    题目链接:CF原网 题目大意:$n$ 个方块排成一排,第 $i$ 个颜色为 $c_i$.定义一个颜色联通块 $[l,r]$ 当且仅当 $l$ 和 $r$ 之间(包括 $l,r$)所有方块的颜色相同.现 ...

  6. Codeforces.392E.Deleting Substrings(区间DP)

    题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...

  7. Codeforces 983B. XOR-pyramid【区间DP】

    LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...

  8. CodeForces - 1025D: Recovering BST (区间DP)

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...

  9. Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理

    转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...

随机推荐

  1. 【BalticOI2003】Gem 题解(树形DP)

    题目大意: 给树上每一个结点赋值(值为正整数),要求相邻结点的权值不能相同.问树上最小权值和.$n\leq 10000$. ------------------------- 设$f[i][j]$表示 ...

  2. 属性集 Properties

    5.1 概述 java.util.Properties 继承于 Hashtable ,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java类使用,比如获 ...

  3. JVM进行篇

                                              结合字节码指令理解Java虚拟机栈和栈帧          栈帧:每个栈帧对应一个被调用的方法,可以理解为一个方法的 ...

  4. R的操作入门熟悉

    产生向量: a=c(1,2,3) //产生 1,2,3向量 a=1:10-1 //产生 0 - 9数字 a=seq(5,20,by=2) // 以2增长 a=seq(5,120,length=10) ...

  5. Android布局的一些属性和开关、创建log图片

    文本的一些属性 android:id="@+id/editText" 给文本的id重命名 android:layout_width="wrap_content" ...

  6. JS 窗口加载与定时器笔记

    bom浏览器对象模型     bom由一系列相关的对象构成并且每个对象都提供了很多方法属性     bom顶级对象是window     bom是浏览器产商在各自浏览器上定义的,兼容性差     wi ...

  7. SSM框架整合Demo

    目前项目大都开始采用SSM结构进行搭建,因为涉及项目比较多,新来的需求都是从现有项目中迁移一份出来进行修改,有的时候两个项目差别还是比较大,并不完全需要原有项目的东西,进行删减也是一项费神费时的事情, ...

  8. Mac 系统更新怎么忽略

    1.在“终端”命令行中输入以下命令: sudo softwareupdate --ignore “macOS Catalina” 2.按回车键,然后输入管理员密码*,然后再次按回车键,以超级用户权限执 ...

  9. 2020-07-16:如何获得一个链表的倒数第n个元素?

    福哥答案2020-07-16: 1.快慢指针.快指针先走n步,然后快慢指针同时走,直到快指针走到尾.2.两次遍历.第一次遍历获取链表长度,然后计算出序号,然后遍历获取序号下的元素.3.数组保存.遍历一 ...

  10. C#LeetCode刷题之#520-检测大写字母(Detect Capital)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3947 访问. 给定一个单词,你需要判断单词的大写使用是否正确. ...