Paint Pearls
Paint Pearls
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009
dp+双向链表优化
看到题目,很自然地可以定义状态:dp[i]表示涂好a[0...i]的字符串,花费的最小代价.
状态转移方程:dp[i]=min(dp[i],dp[j]+num2),其中num=从a[j]到a[i]不同的数字个数.
时间复杂度为O(n2),对于n=50000的数据,明显会T.
于是,我们需要进行优化。注意到状态数无法化简,考虑优化转移复杂度.
当区间[j+1,i]中包含元素a[j]时,无需再经过这个点,直接跳到a[j-1];
即a[j+1]前面略过a[j],直接为a[j-1],使得现序列中各个不同元素只出现一次.
而这种结构可以用双向链表维护.//之前用的是set,T了后查了下clear()是O(n)的,尴尬...
但是当序列为1,2,3,4,5,6,7这种互不相同的元素时,复杂度仍会退化为O(n2),
这时,则需要用到剪枝的技巧:当num2>i时,肯定不会比一个一个涂色方法更优,
由此,复杂度变为O(n3/2)
代码如下:
#include<cstdio>
#include<map>
#define Min(x,y) (x<y?x:y)
#define N 50005
using namespace std;
const int INF=N;
struct List{
int pre,nxt;
List(int _pre=-,int _nxt=-){//ÈÃL[0].preÖ¸Ïò-1
pre=_pre;
nxt=_nxt;
}
}L[N];
int n,a[N],idx,num,dp[N];
map<int,int>mp;
int main(void){
while(~scanf("%d",&n)){
mp.clear();
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
L[i]=List(i-,i+);
}
for(int i=;i<=n;++i){
dp[i]=INF;
if(mp.find(a[i])==mp.end()){
mp[a[i]]=i;
}else{
idx=mp[a[i]];
L[L[idx].pre].nxt=L[idx].nxt;
L[L[idx].nxt].pre=L[idx].pre;
mp[a[i]]=i;
}
for(num=,idx=L[i].pre;idx>=;idx=L[idx].pre,num++){
dp[i]=Min(dp[i],dp[idx]+num*num);
if(num*num>i)break;
}
}
printf("%d\n",dp[n]);
}
}
Paint Pearls的更多相关文章
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
- 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 (动态规划)
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- hdu5009 Paint Pearls[指针优化dp]
Paint Pearls Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
- HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
- 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- HDOJ 5009 Paint Pearls
Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...
随机推荐
- 仿Iconfont-阿里巴巴矢量图标库 搜索动画
效果图如下 style <Style x:Key="BtnSearch" TargetType="{x:Type Button}"> <Set ...
- 【算法专题】工欲善其事必先利其器—— C++ STL中vector(向量/不定长数组)的常用方法总结
#include<iostream> #include<cstdio> #include<string> #include<vector>//不定长数组 ...
- Kattis - Fenwick Tree(树状数组区间更新单点求值)
Fenwick Tree Input The first line of input contains two integers NN, QQ, where 1≤N≤50000001≤N≤500000 ...
- html5之datalist标签
当我看到这个标签的时候,其实我是很愤怒的.因为我以前实现过这个标签的功能,当时是无比的费劲.什么js库呀,function呀.我靠,统统去屎吧,哥有datalist了.那种感觉就好像自己千辛万苦去追去 ...
- Ubuntu 16.04 搭建Android开发环境
1.Installing Java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get inst ...
- C语言:SQLITE3的学习
Sqlite基础学习 一.sqlite的概念 SQLite是一款轻型数据库,是遵守ACID的关系型数据库管理系统,由C语言开发设计.Sqlite的设计目标着眼于嵌入式领域,所以具有占用系统资源低和处理 ...
- openui5中的RESTful实现odata协议
这篇文章第一次看到就一见如故,它是对过去一个时代分布式计算模型的总结,<分布式计算编程模型之RPC>:http://www.infoq.com/cn/news/2016/04/Distri ...
- GTK+2.0学习——第一个GTK程序
#include <gtk/gtk.h> #include <stdio.h> #include <stdlib.h> /* *点击了关闭按钮之后的回调函数 *gt ...
- android平台短视频技术之 视频编辑的经验分享.
android平台短视频技术之 视频编辑的经验分享. 提示一: 各位看官,这里分享的是视频编辑,即剪切/拼接/分离/合并/涂鸦/标记/叠加/滤镜等对视频的编辑操作.不是流媒体网络播放等功能,请注意. ...
- JsSIP.UA.JsSIP 总是返回错误:422 Session Interval Too Small
在JsSIP 中 JsSIP.UA.call 总是 返回错误:422 Session Interval Too Small 关于错详情在这篇文章中解释的比较详尽:http://www.cnblogs. ...