CF 1114 D. Flood Fill

D. Flood Fill

链接

题意:

  一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次。n<=5000

分析:

  区间dp。

  dp[i][j][0/1]表示当前的区间是l~r,把这一段变成一个颜色的最少次数,最后一个改变的颜色是最左边/最右边的一段。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int read() {
    int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;return x*f;
}

const int N = 5005;
int f[N][N][2], a[N], b[N];

int dp(int l,int r,int p) {
    if (l > r) return 0;
    if (l == r) return 0;
    if (f[l][r][p]) return f[l][r][p];
    int &res = f[l][r][p];
    if (p == 0) {
        res = dp(l + 1, r, 0) + (a[l + 1] != a[l]);
        res = min(res, dp(l + 1, r, 1) + (a[r] != a[l]));
    }
    else {
        res = dp(l, r - 1, 0) + (a[l] != a[r]);
        res = min(res, dp(l, r - 1, 1) + (a[r - 1] != a[r]));
    }
    return res;
}
int main() {
    int n = read();
    for (int i = 1; i <= n; ++i) a[i] = read();
    int cnt = 0;
    for (int i = 1; i <= n; ++i) if (a[i] != a[i - 1]) b[++cnt] = a[i];
    n = cnt;
    for (int i = 1; i <= n; ++i) a[i] = b[i];
    cout << min(dp(1, n, 1), dp(1, n, 0));
    return 0;
}

原文地址:https://www.cnblogs.com/mjtcn/p/10361269.html

时间: 2024-11-09 00:49:06

CF 1114 D. Flood Fill的相关文章

Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

任意门:http://codeforces.com/contest/1114/problem/D 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 f

洪水填充(Flood fill)算法

从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法. 因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域. 洪水填充算法接受三个参数:起始节点,目标节点特征和针对提取对象要执行的处理. 目前有许多实现方式,基本上都显式的或隐式的使用了队列或者栈. 洪水填充算法实现最常见有四邻域

USACO The Castle(flood fill)

题目请点我 题解: 这道题真的好蛋疼啊,首先题意不好理解,搞了半天复杂的要死,有那么多要求,还要求那么多东西,做到一半都不想做了...感觉没什么技术含量,还做起来死费劲儿.但是强迫症非得按顺序做题啊,最后还是一点点把它给调出来了,说什么flood fill,其实也就是那么回事,没什么算法上的技巧,就是见招拆招的感觉... 题意搞懂再做题,题意,不谢! 第一步:根据他的规则把房间画出来,遍历一遍把每个节点四面的墙给补上: 第二步:深搜: 目的1:记录深搜的次数,房间数: 目的2:记录深搜的深度,最

SSOJ 2316 面积【DFS/Flood Fill】

题目描述 编程计算由“1”号围成的下列图形的面积.面积计算方法是统计1号所围成的闭合曲线中点的数目. 如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15. 题目大意:对于给定的10*10的01矩阵,请问有多少个0被1包围了?(包围是指不能由上下左右通向边缘)本文来源于OIER博客,原文出处:http://www.oier.cc/ssoj2316%E9%9D%A2%E7%A7%AF/ 解题思路 图形学中Flood Fill是满水法填充,是用来填充区域的.就好比在一个地方一直到

[LeetCode] Flood Fill 洪水填充

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

733. Flood Fill

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

733. Flood Fill 简单型染色问题

[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newC

733. Flood Fill - Easy

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

CodeForces - 1114D Flood Fill (区间dp)

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