任意门:http://codeforces.com/contest/1114/problem/D
2 seconds
256 megabytes
standard input
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.
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.
Print a single integer — the minimum number of the turns needed.
4
5 2 2 1
2
8
4 5 2 2 1 3 5 5
4
1
4
0
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.
题意概括:
给一个序列不同数字代表不同颜色,相同颜色为一连通块,每次操作可以把起点所在的连通块的颜色全部置为任一种颜色。
求将色块的颜色变成全部一样的最少操作次数。
解题思路:
因为颜色相同可以看作一个连通块,所以预处理我们可以先压缩序列。
一、区间DP:
状态:dp[ L ][ R ][ 0 ] 表示将 [ L, R ] 区间的颜色变成 Color[ L ] 所需的最少次数
dp[ L ][ R ][ 1 ] 表示将 [ L, R ] 区间的颜色变成 Color[ R ] 所需的最少次数
转移方程:
IF: Color[ L + 1] == Color [ L ] dp[ L ][ R ][ 0 ] = min( dp[ L ][ R ][ 0 ], dp[ L + 1 ][ R ][ 0 ] );
IF: Color[ L + 1] != Color [ L ] dp[ L ][ R ][ 0 ] = min( dp[ L ][ R ][ 0 ], dp[ L + 1 ][ R ][ 0 ] + 1);
IF: Color[ R -1] == Color [ R ] dp[ L ][ R ][ 1 ] = min( dp[ L ][ R ][ 1 ], dp[ L ][ R - 1 ][ 1 ] );
IF: Color[ R -1] != Color [ R ] dp[ L ][ R ][ 1 ] = min( dp[ L ][ R ][ 1 ], dp[ L ][ R - 1 ][ 1 ] + 1);
AC code( 218 ms):
/// 区间dp
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e3+;
int dp[MAXN][MAXN][];
int num[MAXN];
bool vis[MAXN]; int main()
{
int N = , x = -, cnt = ;
scanf("%d", &cnt);
for(int i = ; i <= cnt; i++){
scanf("%d", &x);
if(x != num[N]) num[++N] = x;
} for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
dp[i][j][] = dp[i][j][] = (i == j?:INF); for(int r = ; r <= N; r++)
for(int l = r; l > ; l--)
for(int it = ; it < ; it++){
int c = (it==?num[l]:num[r]);
if(l > ) dp[l-][r][] = min(dp[l-][r][], dp[l][r][it]+int(c != num[l-]));
if(r < N) dp[l][r+][] = min(dp[l][r+][], dp[l][r][it]+int(c != num[r+]));
} printf("%d\n", min(dp[][N][], dp[][N][])); return ;
}
二、LPS (最长回文序列长度)
试想一下,我们需要变换的点其实是那一些 不在最长回文子序列里的点。
例如: 4 1 3 7 6 3 1 5
最长回文子序列是 1 3 3 1,为了结果最优,我们肯定优先从 7 6 入手,而回文子序列只需要操作一半即可(因为对称),剩下就是处理 4 5。
那么问题就转换为了求LPS:
两种求法:
①直接dp:
AC code(109 ms):
/// LCS
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e3+;
int num[MAXN];
int dp[MAXN][MAXN]; int main()
{
int N = , cnt, x;
scanf("%d", &cnt);
for(int i = ; i <= cnt; i++){
scanf("%d", &x);
if(x != num[N]) num[++N] = x;
}
for(int i = ; i <= N; i++) dp[i][i] = ; for(int i = ; i < N; i++)
for(int j = ; j+i <= N; j++){
if(num[j] == num[j+i]) dp[j][j+i] = dp[j+][j+i-] + ;
else dp[j][i+j] = max(dp[j+][j+i], dp[j][j+i-]);
} int ans = N-(dp[][N]+)/;
printf("%d\n", ans); return ; }
②反向复制原序列,通过原序列求 LCS (最长公共序列)
AC code(78 ms):
/// LCS
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e3+;
int num[MAXN], b[MAXN];
int dp[MAXN][MAXN]; int main()
{
int N = , cnt, x;
scanf("%d", &cnt);
for(int i = ; i <= cnt; i++){
scanf("%d", &x);
if(x != num[N]) num[++N] = x;
}
int E = N;
for(int i = ; i <= N; i++){
b[E--] = num[i];
} for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++){
if(num[i] == b[j]) dp[i][j] = dp[i-][j-]+;
else dp[i][j] = max(dp[i-][j], dp[i][j-]);
}
int ans = N-(dp[N][N]+)/; printf("%d\n", ans); return ; }