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. Web测试转App测试不看不知道

    Web测试 Web通常指的是互联网应用系统,比如税务电子化征管档案系统.金融数据平台.餐饮商家管理后台等等,其实质是C/S的程序. C是Client--客户端,S是Server--服务器. Web中的 ...

  2. 一篇文章快速搞懂什么是GitHub

    导读:什么是GitHub?Git与GitHub之间是什么关系?我们为什么需要版本控制系统?GitHub如何使用?本文将带你一探究竟. 本文字数:1710,阅读时长大约:13分钟 一.什么是版本控制 按 ...

  3. TCP学习指北

    限于博主水平有限不敢说指南,但应该能够避免刚学TCP的同学出现找不着北的情况. TCP与UDP的区别 区别: UDP是无连接的,而TCP是面向连接的,传数据前要先建立连接. UDP可以一对多,多对多通 ...

  4. C#LeetCode刷题之#69-x 的平方根(Sqrt(x))

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3848 访问. 实现 int sqrt(int x) 函数. 计算 ...

  5. P、NP、NPC问题详解

    转载地址 https://blog.csdn.net/bcb5202/article/details/51202589 P.NP.NPC 概念 > P问题:能够在多项式时间内解决的决策问题. - ...

  6. 调试备忘录-J-Link RTT的使用(原理 + 教程 + 应用 + 代码)

    MCU:STM32F407VE MDK:5.29 IAR:8.32 目录--点击可快速直达 目录 写在前面 什么是RTT? RTT的工作原理 RTT的性能 快速使用教程 高级使用教程 附上测试代码 2 ...

  7. 关于初次使用Thymeleaf遇到的问题 2020-08-11

    关于初次使用Thymeleaf遇到的问题 环境: IDEA :2020.1 Maven:3.5.6 SpringBoot: 2.3.2 原做法: 按照视频教程,导入依赖,并修改报的版本为3.0.9,适 ...

  8. SpringBoot ---yml 整合 Druid(1.1.23) 数据源

    SpringBoot ---yml 整合 Druid(1.1.23) 数据源 搜了一下,网络上有在配置类写 @Bean 配置的,也有 yml 配置的. 笔者尝试过用配置类配置 @Bean 的方法,结果 ...

  9. 用C++基础语句写一个五子棋游戏

    (这是一个颜色会变化的呦) #include <iostream> using namespace std; int b[][]; int n; int m; void qipan() { ...

  10. github Repository not found 解决办法

    git pull的时候遇到下面的报错. remote: Repository not found fatal: repository 'https://github.com/MyRepo/projec ...