Flood Fill(区间dp)

探讨一个染色游戏问题,目标是最少操作次数将一排不同颜色的方块统一成单一颜色。通过动态规划算法,寻找最优策略,考虑从两端开始改变颜色到中间,或反之。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://cn.vjudge.net/problem/CodeForces-1114D

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

Note

In the first example, a possible way to achieve an optimal answer is to pick square with index 22 as the starting square and then play as follows:

  • [5,2,2,1]
  • [5,5,5,1]
  • [1,1,1,1]

In the second example, a possible way to achieve an optimal answer is to pick square with index 55 as the starting square and then perform recoloring into colors 2,3,5,42,3,5,4in that order.

In the third example, the line already consists of one color only.

题意:连续的几个颜色相同的格子称为一个连通块。选一个点为起点,每个操作是把所在连通块变一个颜色,求把整个区间染成同色需要的最少操作数。(注意,每次只能改变所在连通块的颜色,不能任选连通块,除了最开始时)

对于区间[L,R],最优的方案要么是全变成L处的颜色,要么全变成R处的颜色

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
const int N=5e3+5;
int c[N];
int dp[N][N][2];
inline int judge(int a,int b){
	return c[a]==c[b]?0:1;
}
int dfs(int l,int r,int flag){
	if(dp[l][r][flag]!=-1) return dp[l][r][flag];
	if(l==r) return 0;
	int ans=N;
	if(flag==0){
		ans=min(ans,dfs(l+1,r,0)+judge(l,l+1));
	    ans=min(ans,dfs(l+1,r,1)+judge(l,r));
	}
	else{
		ans=min(ans,dfs(l,r-1,0)+judge(l,r));
	    ans=min(ans,dfs(l,r-1,1)+judge(r-1,r));
	}	
	return dp[l][r][flag]=ans;
}
int main(){
	int n;
	scanf("%d",&n);
	c[0]=N;
	int m=0;
	for(int i=1;i<=n;i++) {
		int x;
		scanf("%d",&x);
		if(x!=c[m]) c[++m]=x;
	}
	n=m;
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			dp[i][j][0]=dp[i][j][1]=-1;
		}
	}
    int ans=min(dfs(1,n,0),dfs(1,n,1));
    printf("%d\n",ans);
	return 0;
}

 

### Flood Fill算法的实现 Flood Fill是一种用于填充连通区域的颜色或纹理替换方法,在计算机图形学和图像处理领域广泛应用。该算法通常有两种主要形式:四向连接(上下左右)以及八向连接(包括对角线)。对于每一个像素点,如果其颜色匹配起始颜色,则将其更改为新颜色并继续检查相邻像素。 #### 实现方式 一种常见的递归版本如下所示: ```cpp void floodFill(int x, int y, Color oldColor, Color newColor) { // 如果当前坐标超出边界范围则返回 if (x < 0 || x >= width || y < 0 || y >= height) return; // 获取当前位置的颜色 Color currentColor = getImagePixel(x, y); // 若当前位置不是旧颜色或者已经是新的目标颜色,则停止操作 if (currentColor != oldColor || currentColor == newColor) return; // 设置为目标颜色 setImagePixel(x, y, newColor); // 对四个方向上的邻居节点调用flood fill函数 floodFill(x + 1, y, oldColor, newColor); // 右侧 floodFill(x - 1, y, oldColor, newColor); // 左侧 floodFill(x, y + 1, oldColor, newColor); // 下方 floodFill(x, y - 1, oldColor, newColor); // 上方 } ``` 为了提高效率,可以采用栈结构来代替递归来避免深度过大引起的问题[^1]。 #### 应用场景 - **绘画软件中的油漆桶工具**:允许用户点击画布上任意一点,并自动将相同颜色区域内所有像素替换成指定的新颜色。 - **游戏开发中地图编辑器的功能之一**:帮助开发者快速修改大面积地形特征而不必逐一手动调整每个单元格属性。 - **医学影像分析**:识别特定器官轮廓内部空间,辅助医生进行诊断工作。 - **缺陷检测系统**:通过标记产品表面异常斑块位置以便后续质量检验人员审查确认。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值