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的更多相关文章

  1. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  2. HDU 5009 Paint Pearls 双向链表优化DP

    Paint Pearls Problem Description   Lee has a string of n pearls. In the beginning, all the pearls ha ...

  3. HDU 5009 Paint Pearls (动态规划)

    Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  4. hdu5009 Paint Pearls[指针优化dp]

    Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

  6. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

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

  8. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  9. HDOJ 5009 Paint Pearls

    Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...

随机推荐

  1. TCP报文中的SYN,FIN,ACK,PSH,RST,URG

    TCP的三次握手是怎么进行的:发送端发送一个SYN=1,ACK=0标志的数据包给接收端,请求进行连接,这是第一次握手:接收端收到请求并且允许连接的话,就会发送一个SYN=1,ACK=1标志的数据包给发 ...

  2. java基础练习 11

    import java.util.Scanner; public class Eleventh { /*给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. (5 分数)*/ p ...

  3. VirtualBox 主机与虚拟机互通

    文章转载:http://www.cnblogs.com/HD/p/4011323.html 网络要设置才能互通 注意:不启动Linux系统的时候,设置网络 使用VirtualBox的主机与虚拟机相互通 ...

  4. ActiveMQ的简单例子应用

    ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式.下面介绍下ActiveMQ的使用: 一.环境的搭建 首先我们需要下载ActiveMQ的安装包,下载地址http:/ ...

  5. 巧用ajax请求服务器加载数据列表时提示loading

    我们利用weui.js中的weui.loading为效果,ajax的beforeSend与complete方法,做一个加载数据时会有几秒的 loading... 要在页面需要加载的JS文件: < ...

  6. C语言中的位运算的技巧

    一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...

  7. C#-实验3

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. [转]RadStudio DELPHI/C++ BUILDER Berlin 10.1 Update2安装破解教程

    源链接:http://bbs.fishc.com/thread-76730-1-1.html 免责声明:本教程所有资源均为学习交流之用,软件和资料版权归原开发公司所有,请于下载后24小时内删除,禁止用 ...

  9. android log4j日志管理的使用

    以下为log4j1的日志管理,在android 6.0 一下能正常使用,时候更加高级的胃log4j2,持续跟新 android中的log4j日志文件使用需要两个包,我们不需要进行配置文件的配置,一切都 ...

  10. C# 根据IP获取省市

    /// <summary> /// 根据IP获取省市 /// </summary> public void GetAddressByIp() { string ip = &qu ...