CF1114D Flood Fill

题目

题目描述

You are given a line of nn colored squares in a row, numbered from 11 to nnn from left to right. The iii -th square initially has the color cic_ici​ .Let’s say, that two squares iii and jjj belong to the same connected component if ci=cj​c_i = c_j​ci​=cj​​ , and ci=ckc_i = c_kci​=ck​​ for all kkk satisfying i<k<ji < k < ji<k<j . In other words, all squares on the segment from iii to jjj should have the same color.For example, the line [3,3,3][3, 3, 3][3,3,3] has 11 connected component, while the line [5,2,4,4][5, 2, 4, 4][5,2,4,4] has 333 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 nnn ( 1≤n≤50001\le n \le50001≤n≤5000 ) — the number of squares.The second line contains integers c1,c2,…,cnc_1, c_2, \ldots, c_nc1​,c2​,…,cn​​ ( 1≤ci≤50001 \le c_i \le 50001≤ci​≤5000 ) — the initial colors of the squares.

输出格式

Print a single integer — the minimum number of the turns needed.

题意翻译

n 个方块排成一排,第 iii 个颜色为 cic_ici​​。定义一个颜色联通块 [l,r][l,r][l,r] 当且仅当 lll 和 rrr 之间(包括 l,rl,rl,r)所有方块的颜色相同。现在你可以选定一个起始位置 ppp,每次将 ppp 所在颜色联通块的所有方块颜色改成另一种。这个操作可能将多个颜色联通块合并成一个。问最少要多少步,能让 [1,n][1,n][1,n]变成一个颜色联通块

数据范围

1≤n,ci​≤5000。1≤n,ci​≤5000。1≤n,ci​≤5000。

输入输出样例

输入 #1
4
5 2 2 1
输出 #1
2
输入 #2
8
4 5 2 2 1 3 5 5
输出 #2
4
输入 #3
1
4
输出 #3
0

说明/提示

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

  • [5,2,2,1][5, 2, 2, 1][5,2,2,1]
  • [5,5,5,1][5, 5, 5, 1][5,5,5,1]
  • [1,1,1,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 555 as the starting square and then perform recoloring into colors 2,3,5,42, 3, 5, 42,3,5,4 in that order.In the third example, the line already consists of one color only.

原题链接

题解

思路

这道题是说的选定一个位置, 接着不停地改变这个位置所在颜色联通块的颜色, 直到所有位置颜色都相同。这里注意选定的位置不能改变, 只能在同一个地方进行改变颜色的操作。这道题的算法比较好分析, 可以进行区间合并, 那肯定就是区间dp了, 但是看到数据范围, nnn最大是5000, 一般的区间dp都是
O(n3)O(n^3)O(n3), 这是肯定会超时的, 所以我们应该想办法省掉一层循环。最外层循环是枚举的区间长度, 也就是区间dp的阶段, 第二层循环是枚举的起始位置,这两个很明显都不能去掉, 看来看去能够省下来的只能是最后枚举划分位置的那一层循环了。 接下来我们就要想如何省了。 我们通过读题, 知道了进行改变颜色的位置只能是在同一处, 所以我们枚举的每个两边不相等区间都可以由通过改变其中长度少1的区间变为剩下的那一个位置而得到, 既是
dp[l][r]=min(dp[l+1][r]+1,dp[l][r−1]+1)dp[l][r] = min (dp[l + 1][r] + 1, dp[l][r - 1] + 1)dp[l][r]=min(dp[l+1][r]+1,dp[l][r−1]+1)
而当两头一样的话就只需要改变内部, 使这个区间除去两端的区间颜色改变成两端一样的区间, 既是
dp[l][r]=dp[l+1][r−1]+1dp[l][r] = dp[l + 1][r - 1] + 1dp[l][r]=dp[l+1][r−1]+1
不过此时又有问题了, 如果里面还是一个两头相等且和外面一样的区间, 这样就不能加1了, 比如2,2,2,22, 2, 2, 22,2,2,2这里一次都不需要改, 可是直接执行上面的方法就会输出222, 这时候就需要在输入时就把所有颜色联通块的长度压缩成1。
方法:

for (int i = 1; i <= n; i++){scanf ("%d", &s[i]);if (s[i] == s[i - 1]){i --;n --;}
}

这样就可以避免上述情况发生

运用算法

区间dp区间dp区间dp

代码

#include <cstdio>
#include <algorithm>
using namespace std;#define MAXN 5000int s[MAXN + 5], dp[MAXN + 5][MAXN + 5];int main(){int n;scanf ("%d", &n);for (int i = 1; i <= n; i++){scanf ("%d", &s[i]);if (s[i] == s[i - 1]){//压缩联通块 i --;n --;}}//dp[i][i]就是0, 所以不用初始化 for (int i = 2; i <= n; i++){//枚举长度 for (int l = 1; l <= n - i + 1; l++){int r = l + i - 1;if (s[l] == s[r]){//如果两端相等, 就直接等于去除两端的区间加1 dp[l][r] = dp[l + 1][r - 1] + 1;}else {//否则比较从哪个变过来小 dp[l][r] = min (dp[l + 1][r], dp[l][r - 1]) + 1; }}}printf ("%d", dp[1][n]);//输出整个区间的变化次数 return 0;
}

CF1114D Flood Fill相关推荐

  1. LeetCode Number of Islands(flood fill)

    问题:给出一个由0和1组成的二维网格图(m*n),1表示陆地,0表示水.要求统计有多少块陆地 思路:常见的flood fill算法有三种,深度优先搜索.广度优先搜索以及广度扫描法.广度扫描法其实原理与 ...

  2. flood fill算法

    flood fill算法实现有三种形式 1.depth first search 2.breadth first search 3.breadth first scan 基本思想是找到还没有分配com ...

  3. usaco The Castle(flood fill)

    问题:城堡有n*m个方块组成,方块四周可能有墙,分别用1(W),2(N),4(E),8(S)来表示,每个方块由一个数字来表示,由四周的分布的墙值和来表示.要求求出城堡有多少个房间,最大房间的大小及删除 ...

  4. 算法提高课-搜索-Flood fill算法-AcWing 1106. 山峰和山谷:flood fill、bfs

    题目分析 来源:acwing 分析:这道题还是flood fill算法的应用,不同点在于八个方向扫描,习惯性采用二重循环来扫描周围的8个方向:其次,这里需要统计周围比它高的和比它矮的,这点用bool变 ...

  5. 算法提高课-搜索-Flood fill算法-AcWing 1098. 城堡问题:flood fill、bfs

    题目分析 来源:acwing 分析:找房间个数,也就是找连通的个数. 样例画出来的房间个数如下图:其中'|' 和'-'不是墙,只有#是墙. 分析:这题不用建图,直接bfs(flood fill)来做, ...

  6. 算法提高课-搜索-Flood fill算法-AcWing 1097. 池塘计数:flood fill、bfs

    Flood fill 算法简介: 像洪水一样,一圈一圈往外蔓延,像bfs. flood fill 算法可以在线性复杂度内,找到某个点所在的连通块. 题目分析 来源:acwing ac代码 #inclu ...

  7. C语言flood fill 泛洪算法(附完整源码)

    C语言flood fill 泛洪算法 泛洪算法引出 C语言flood fill 泛洪算法完整源码(定义,实现,main函数测试) 泛洪算法引出 给定2D屏幕,像素的位置和要填充的颜色的新值,请用新颜色 ...

  8. 洪水填充算法_洪水填充(Flood fill)算法

    洪水填充(Flood fill)算法 从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或 ...

  9. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

最新文章

  1. 自然语言处理系列篇——关键词智能提取
  2. windbg-调试技巧(定长、不定长参数thiscall平衡堆栈方式不同)
  3. 二分法查找 - python实现
  4. Selenium操作页面元素
  5. c web mysql数据库_C语言操作MySQL数据库
  6. 关于贝叶斯公式的解释,通俗易懂(转载)
  7. 使用ESAPI 解决veracode 漏洞
  8. 【190105】VC++ 家庭理财系统1.0(Access)源码源代码
  9. 瀑布流效果Demo总结(4)之基于jquery+masonry.js的实现
  10. 联通光猫 - KD-YUN-811E - 管理员密码破解
  11. 鸿蒙生死印是谁的,逆天邪神:鸿蒙印的器灵还存在,或许云澈将知道些关于远古的秘密...
  12. 学习笔记:Adaptive Convolutional Kernels
  13. vue框架如何将SPA项目改为SSR项目
  14. [文献阅读]—一篇不错的低资源机器翻译综述(Neural Machine Translation for Low-Resource Languages: A Survey)
  15. 不用工具,如何快速计算文件的MD5?
  16. [RK3399][Android7.1] 调试笔记 --- Audio codec时钟源从BCLK1获取
  17. 结对编程项目——最长英语单词链
  18. [转贴]电脑使用者的眼睛保护须知
  19. error LNK1207:-XXX.pdb”中的 PDB 格式不兼容;请删除并重新生成
  20. iOS —— 触摸事件传递及响应与手势

热门文章

  1. Spring Boot SOAP Client – WebServiceTemplate 案例
  2. ABAQUS 自适应网格技术_51CAE_新浪博客
  3. OpenFOAM动网格技术介绍【转载】
  4. Cortex-A7中断详解(二)
  5. 推荐用于学习RN原生模块开发的开源库—react-native-ble-manager
  6. python1加到100_python实现1加到100
  7. 等差数列和特殊矩阵压缩公式/下标计数公式的应用
  8. 服务器内部转发和客户端重定向
  9. 浅谈虚拟磁带库备份的性能问题
  10. curl静态库的编译与使用