HDU-5009 Paint Pearls 动态规划 双向链表
题目链接:https://cn.vjudge.net/problem/HDU-5009
题意
给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数。
求分割区间的最小代价。n<=5e4
例:1 3 3
答:2
思路
今天刚讲的dp题目,这次讲课收益非常,写个题解。
状态和转移是dp[i]=min(dp[j]+cnt[j-1][i]^2)
会发现这样绝对超时,又发现这里的j可以贪心的跳着选择(因为种数相同时,左端点应越小越好)。
这里的跳着选择想了半天没什么思路,又是最后看了题解-_-
双向链表在排除某一元素时非常有用,以前只用过一次,这次真的是感受到这玩意的通用之处了(话说这个东西应该非常方便,继并查集之后的又一轻量级数据结构)
提交过程
| TLE | 注意当区间的种类超过sqrt(i)时,不如一个一个选,跳出即可 |
| WA | 注意边界dp[0]=0, 而非dp[1]=1 |
| AC |
代码
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5e4+20, INF=0x3f3f3f3f;
int n, clr[maxn], pre[maxn], nex[maxn];
int dp[maxn];
int main(void){
while (scanf("%d", &n)==1 && n){
for (int i=1; i<=n; i++) scanf("%d", &clr[i]);
for (int i=1; i<=n; i++) dp[i]=INF;
for (int i=1; i<=n; i++)
pre[i]=i-1, nex[i]=i+1;
pre[0]=-1; nex[n]=-1;
dp[0]=0;
map<int, int>id;
for (int i=1; i<=n; i++){
if (!id.count(clr[i])) id[clr[i]]=i;
else{
int idx=id[clr[i]];
pre[nex[idx]]=pre[idx];
nex[pre[idx]]=nex[idx];
id[clr[idx]]=i;
}
for (int k=pre[i], cnt=1; k!=-1; k=pre[k], cnt++){
dp[i]=min(dp[i], dp[k]+cnt*cnt);
if (cnt*cnt>i) break;
}
}
printf("%d\n", dp[n]);
}
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 686ms | 3444kB | 975 | G++ | 2018-08-13 08:37:10 |
HDU-5009 Paint Pearls 动态规划 双向链表的更多相关文章
- HDU 5009 Paint Pearls (动态规划)
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- hdu 5009 Paint Pearls
首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...
- hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化
http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...
- HDOJ 5009 Paint Pearls
Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
随机推荐
- BAT三家互联网公司哪家更注重用户体验?
这几天百度的用户体验又成了设计圈关注的对象,李彦宏好不容易刷出来的好感度一下子被打入了冰点,通过此次事件,不难看出现在的互联网用户对于产品的体验要求越来越高,作为一名美图秀秀级别选手,很难领悟“好设计 ...
- OCR文字识别软件FineReader系列产品双十一特惠!
17年的双十一,似乎比以往来的更早一些,说是双十一,这不,从十月里就开始了各种宣传,各种造势,说到底还是影响力太大,关注人群太多,优惠力度太劲爆,简直让人不注意都不行啊!对于ABBYY来说,给用户来点 ...
- 为什么密信MeSince采用S/MIME加密?
S/MIME是Secure/Multipurpose Internet Mail Extensions (安全多用途互联网邮件扩展协议)的缩写,是采用PKI技术的用数字证书给邮件主题签名和加密的国标标 ...
- TensorFlow+实战Google深度学习框架学习笔记(6)----神经网络经典损失函数
1.分类问题(交叉熵): (1)模型: (2)代码1: 其中,tf.clip_by_value是将一个张量的数值限制在一个范围之内,若小于1e-10则赋值为1e-10,若大于1.0则赋值为1,这样避免 ...
- css font-family 字体组
介绍图片来自: http://www.runoob.com/cssref/css-websafe-fonts.html
- jpa自定义条件分页查询
主要依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- git 简单理解
现在git这个版本控制大行其道,弄了半天大概理解了一下他的工作原理. 使用流程 1,安装git ,小乌龟,小乌龟汉化(在设置里面第一项,检查更新,下载中文包安装) 2,设置 小乌龟 ->git ...
- 【henuacm2016级暑期训练-动态规划专题 C】Little Girl and Maximum XOR
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑r最后的二进制形式为 1xxxxx 那么我们肯定想让第一个最高位的1保留. 因此我们选取的另外一个数字 一定是 0xxxxx的形 ...
- Maven中的parent定义的dependency,其中继承者是可以直接使用parent中的Maven Dependencies的。
Maven中的parent定义的dependency,其中继承者是可以直接使用parent中的Maven Dependencies的. packagin要选择jar,parent project要选择 ...
- elasticsearch的javaAPI之index
Index API 原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html ...