codeforces #538 D. Flood Fill(最长公共子序列,最长回文子序列)

本文探讨了一个有趣的游戏“洪水填充”,目标是最小化改变整行颜色所需的回合数。通过寻找最长回文子序列的方法,实现对整个数组进行最少次数的颜色统一。

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

D. Flood Fill

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

4
5 2 2 1

output

Copy

2

input

Copy

8
4 5 2 2 1 3 5 5

output

Copy

4

input

Copy

1
4

output

Copy

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,2,2,1]
  • [5,5,5,1][5,5,5,1]
  • [1,1,1,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,4 in that order.

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

题意:

有个数组,我们可以每一步将一个元素向左或者向右边不同的一个元素传递,如果左边或者右边的连续相同,则可以一直贯通,求完成整个数组需要的最少步数

[5,2,2,1]->[5,5,5,1]->[1,1,1,1]

解析:

因为如果相同,则可以连续贯通,所以我们删除相邻的相同的,使得a[i]!=a[i+1]

我们需要找到一个起始点,起始点两边的子序列对称,找到对称数量最多的情况

也就是求最长回文子序列

我们可以将a反转,求a与b的最长公共子序列,最长回文子序列=(a&b)最长公共子序列/2

原本就有一个,n还要-1

答案就是n-1-(a&b)最长公共子序列/2.

ac:

#include<bits/stdc++.h>
#define MAXN 5005
using namespace std;

int n,x;
int dp[MAXN][MAXN]={0};
int a[MAXN],b[MAXN];

int main()
{
    scanf("%d",&n);
    int j=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        if(i>0)
        {
            if(x!=a[j])
                j++,a[j]=x;
        }
        else j++,a[j]=x;
    }
    for(int i=j;i>=1;i--)
        b[i]=a[j-i];
    n=j;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i]==b[j])
                dp[i][j]=dp[i-1][j-1]+1;
            else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }

    printf("%d\n",n-1-dp[n][n]/2);
    return 0;
}

lsp(最长回文子序列)

#include<bits/stdc++.h>
#define N 5005
using namespace std;

int dp[N][N]={0};
int a[N]={0};

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n+1;i++)
        scanf("%d",&a[i]);
    n=unique(a+1,a+n+1)-a-1;
    for(int i=1;i<n;i++)
    {
        for(int j=1;j+i<=n;j++)
        {
            if(a[j]==a[j+i])
                dp[j][j+i]=dp[j+1][j+i-1]+1;
            else dp[j][j+i]=min(dp[j+1][j+i]+1,dp[j][j+i-1]+1);
        }
    }
    cout<<dp[1][n]<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值