hdu5009 Paint Pearls[指针优化dp]
Paint Pearls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3239 Accepted Submission(s): 1052
In each operation, he selects some continuous pearls and all these pearls will be painted to their target colors. When he paints a string which has k different target colors, Lee will cost k2 points.
Now, Lee wants to cost as few as possible to get his ideal string. You should tell him the minimal cost.
For each test case, the first line contains an integer n(1 ≤ n ≤ 5×104), indicating the number of pearls. The second line contains a1,a2,...,an (1 ≤ ai ≤ 109) indicating the target color of each pearl.
1 3 3
10
3 4 2 4 4 2 4 3 2 2
7


/*
f[i]表示涂完前適个所化的最小代价
f[i]=min(f[j]+num(j+1,i)^2);{0<=j<i}
双向链表优化+剪枝后时间复杂度:O(n√n)
*/
#include<map>
#include<cstdio>
#include<iostream>
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=1e5+;
int n,a[N];
int pre[N],nxt[N];
map<int,int>mp;
int f[N];
int main(){
while(~scanf("%d",&n)){
mp.clear();fill(f,f+n+,2e9);f[]=;
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=n;i++) pre[i]=i-,nxt[i]=i+;
//双向链表删点好神奇
for(int i=,tmp;i<=n;i++){
if(!mp.count(a[i])) mp[a[i]]=i;
else{
tmp=mp[a[i]];
nxt[pre[tmp]]=nxt[tmp];
pre[nxt[tmp]]=pre[tmp];
mp[a[i]]=i;
}
for(int j=pre[i],cnt=;~j;j=pre[j]){
cnt++;
f[i]=min(f[i],f[j]+cnt*cnt);
if(cnt*cnt>n) break;
}
}
printf("%d\n",f[n]);
}
return ;
}
hdu5009 Paint Pearls[指针优化dp]的更多相关文章
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
- poj 1260 Pearls 斜率优化dp
这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...
- hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化
http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...
- HDU1300 Pearls —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-1300 Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- HDOJ 1300 Pearls 斜率优化dp
原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题意: 题目太长了..自己看吧 题解: 看懂题目,就会发现这是个傻逼dp题,斜率优化一下就好 代 ...
- HDU-5009 Paint Pearls 动态规划 双向链表
题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...
- 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(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
随机推荐
- Hibernate Query Language查询:
Hibernate Query Language查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Language)查询提供 ...
- 第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型
第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型 Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value)name ...
- Java集合类相关面试题
1.Collection和Collections的差别 java.util.Collection 是一个集合接口,Collection接口在Java类库中有非常多详细的实现.比如List.Set ja ...
- 一个不错的在线的js调试器
一个不错的在线的js调试器,可见即可得: http://jsbin.com/
- windows 下安装perl Tk 模块
首先,安装activeperl ,安装过程中勾选自动添加PATH环境变量,这样安装后就不需要自己手动修改PATH环境变量: 通过cmd 调出命令行窗口,输入ppm ,然后回车,就开启了perl 的包管 ...
- Js Object转化为json,json转Object
var obj={x:10,y:50};var t= JSON.stringify(obj);console.log(typeof t);var gg= JSON.parse(t);console.l ...
- struts2将数据通过Json格式显示于EasyUI-datagrid数据表格
1.搭建ssh开发环境 2.写好Dao.service等方法 3.建立DTO数据传输对象: package com.beichende.sshwork.user.web.dto; import jav ...
- u3d性能优化
原文地址:http://blog.csdn.net/molti/article/details/8520418 性能优化需要从多方面入手,大家在项目中遇到的问题还是很普遍的,欢迎大家补充. 图形方面: ...
- Solr学习之一 --------环境搭建
一.准备工具 下载Solr,以目前最新版solr-6.1.0为例 准备servlet容器,Tomcat,Jetty,Resin之类.以Tomcat7为例 二.开始动手 将solr解压出来,在sol ...
- VC++ 6.0开发套件(自己收藏!)
安装镜像ISO VC++ 6.0_SP6_Win7企业版(中英文集成).iso MSDN安装镜像ISO MSDN_Oct_200 ...