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=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int f[N][N][], a[N], b[N]; int dp(int l,int r,int p) {
if (l > r) return ;
if (l == r) return ;
if (f[l][r][p]) return f[l][r][p];
int &res = f[l][r][p];
if (p == ) {
res = dp(l + , r, ) + (a[l + ] != a[l]);
res = min(res, dp(l + , r, ) + (a[r] != a[l]));
}
else {
res = dp(l, r - , ) + (a[l] != a[r]);
res = min(res, dp(l, r - , ) + (a[r - ] != a[r]));
}
return res;
}
int main() {
int n = read();
for (int i = ; i <= n; ++i) a[i] = read();
int cnt = ;
for (int i = ; i <= n; ++i) if (a[i] != a[i - ]) b[++cnt] = a[i];
n = cnt;
for (int i = ; i <= n; ++i) a[i] = b[i];
cout << min(dp(, n, ), dp(, n, ));
return ;
}

CF 1114 D. Flood Fill的更多相关文章

  1. CF 538 D. Flood Fill 递归 区间DP

    link:https://codeforces.com/contest/1114/problem/D 题意: 给定一个数组,有不同的颜色,你可以从任意一个位置开始,改变颜色,相邻的是同一种颜色的位子的 ...

  2. 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 ...

  3. 图像处理之泛洪填充算法(Flood Fill Algorithm)

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

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

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

  5. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  6. [Swift]LeetCode733. 图像渲染 | Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  7. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

  8. [LeetCode&Python] Problem 733. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  9. LeetCode - Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

随机推荐

  1. maskView遮罩中多张图片的动画

    maskView遮罩中多张图片的动画 说明 用多张图片做遮罩效率极高,非常好理解,而且极其美观! 效果图 素材 源码 // // ViewController.m // FeedBack // // ...

  2. 铁乐学python_Day43_协程

    铁乐学python_Day43_协程 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位. 按道理来说我们已经算是把cpu的利用率提高很多了. ...

  3. 17级-车辆工程-周金霖 计算机作业 MP4音乐网站

  4. 《关于oracle数据库的勒索病毒的预警》

    近日,接部分机构反馈和安全厂商提醒,针对oracle数据库的勒索病毒攻击数量增加.该病毒存在较长潜伏期,会根据数据库实例创建时间距今是否满足1200天决定是否发起攻击.攻击通过执行恶意SQL脚本,加密 ...

  5. spark-submit提交参数设置

    /apps/app/spark-1.6.1-bin-hadoop2.6/bin/spark-submit --class com.zdhy.zoc2.sparksql.core.JavaSparkSq ...

  6. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

  7. 张高兴的 Windows 10 IoT 开发笔记:FM 电台模块 KT0803L

    This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...

  8. PHP设计模式系列 - 观察者模式

    观察者模式 观察者设计模式能够更便利创建和查看目标对象状态的对象,并且提供和核心对象非耦合的置顶功能性.观察者设计模式非常常用,在一般复杂的WEB系统中,观察者模式可以帮你减轻代码设计的压力,降低代码 ...

  9. Echarts 曲线数少于图例数解决方法

    在上一篇文章 Echarts 多曲线“断点”问题解决方法 中说到了Angular 项目中要使用 Echarts 的方法. 说明了自己解决当“每一条曲线的横坐标不相同”时,在各条曲线上,它们的值采用数组 ...

  10. DA-GAN技术【简介】【机器通过文字描述创造图像】

    [题外话:今天上课我做了一个关于DA-GAN技术的ppt演讲,写一点东西留念一下...] 转载请注明出处:https://www.cnblogs.com/GraceSkyer/p/9107471.ht ...