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 ...
随机推荐
- datanode启动失败
当我动态加入一个hadoop从节点的之后,出现了一个问题: [root@hadoop current]# hadoop-daemon.sh start datanode starting datano ...
- svg 添加超链接
<svg> <a xlink:href="http://www.w3.org//Graphics//SVG//Overview.htm8"> ...
- springmvc拦截器的配置、使用
springmvc拦截器的配置.使用:1.自定义拦截器,实现HandlerInterceptor接口. package com.bybo.aca.web.interceptor; import jav ...
- html块状元素、内联元素
html块状元素.内联元素 原文在这 块级元素的分类 块级元素按照其应用于结构还是内容分为三种:结构化块状元素,终端块状元素,多目标块状元素. 一.结构化块状元素 这类元素用于构造文档的结构,一个好的 ...
- jquery 表格排序,实时搜索表格内容
jquery 表格排序,实时搜索表格内容 演示 XML/HTML Code <table class="table-sort"> <thead> < ...
- 【转】MFC 之CEvent
event是用来同步不同线程的.一旦一个线程结束了自己对全局资源的使用,他通过调用SetEvent通知别人可以使用了.如果这个被删了,其他线程将被阻塞 当一个线程里调用了::WaitForSingle ...
- Extracting and composing robust features with denosing autoencoders 论文
这是一篇发表于2008年初的论文. 文章主要讲了利用 denosing autoencoder来学习 robust的中间特征..进上步,说明,利用这个方法,可以初始化神经网络的权值..这就相当于一种非 ...
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
这个特性是andorid4.4支持的,最少要api19才干够使用.以下介绍一下使用的方法,很得简单: 添加一个demo源代码: https://github.com/ws123/StatusDemo ...
- 如何在vs2008安装64位编译器
1.打开D:\Microsoft Visual Studio 9.0\Microsoft Visual Studio 2008 Professional Edition - CHS setup.exe ...
- mysql数据库,什么是数据库的全备份?
需求描述: 今天要做mysql数据库的全备份,那么就要弄清楚一个概念,到底什么是数据库的全备份呢. 概念解释: 数据库的全备份,就是在一个给定的时间点,对于mysql服务器管理的所有的数据进行备份. ...