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. idea2019配置gradle详解_Java学习之——Gradle的安装配置、IDEA中创建Gradle的Java项目...
  2. Linux System Programming --Chapter Five
  3. OpenCV配置选项参考
  4. 阿里云centos云服务器 - 网站搭建教程
  5. Linux基础(2)--Linux常用shell命令
  6. 【华为云技术分享】STM32L476移植华为LiteOS系列教程---开发前的准备 2
  7. python实例属性与类属性_Python类属性与实例属性用法分析
  8. expect脚本中,变量的写法
  9. 1月16日学习内容整理:存储库MongoDB之pymongo模块
  10. *揭秘!阅读类APP如何实现自动阅读赚钱**
  11. 大数据应用能力层次模型
  12. 将球放入盒中的方法数总结(球盒模型问题)
  13. Tegra Nano上手
  14. 含有非期望产出的ZSG-DEA模型
  15. 论文阅读笔记----《Explaining Answers with Entailment Trees》
  16. Windows Support Tools
  17. java操作excel的工具
  18. zblogphp 广告联盟_天兴工作室:广告位大全插件(网站各种广告位集合效果)
  19. GPT-3让人怀疑人生!惊艳了世界!道翰天琼认知智能机器人api接口平台为您解密!
  20. Oracle 表解锁

热门文章

  1. ip地址、域名、DNS解析服务器
  2. 2018双一流排名 计算机,2018中国双一流学科排名出炉,北京大学第一
  3. DM368开发 -- 编码并实时播放
  4. DAVINCI DM365-DM368开发攻略——U-BOOT-2010.12及UBL的移植
  5. Comparable和Comparator的用法和区别
  6. 计算机怎样选定硬盘,电脑如何给硬盘分区
  7. IPFS如何构建下一代互联网?
  8. Vue中实现div编辑效果,及contenteditable设置为plaintext-only与true的区别
  9. 挖坑了挖坑了,走过路过不要错过 。。。
  10. Linux:cd命令详解