Paint Pearls

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3239    Accepted Submission(s): 1052

Problem Description
Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help.

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.

 
Input
There are multiple test cases. Please process till EOF.

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.

 
Output
For each test case, output the minimal cost in a line.
 
Sample Input
3
1 3 3
10
3 4 2 4 4 2 4 3 2 2
 
Sample Output
2
7
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 

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

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

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

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

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

  3. poj 1260 Pearls 斜率优化dp

    这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...

  4. hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...

  5. HDU1300 Pearls —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-1300 Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  6. HDOJ 1300 Pearls 斜率优化dp

    原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题意: 题目太长了..自己看吧 题解: 看懂题目,就会发现这是个傻逼dp题,斜率优化一下就好 代 ...

  7. HDU-5009 Paint Pearls 动态规划 双向链表

    题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...

  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. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

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

随机推荐

  1. redis的window客户端下载地址

    这里是window的版本,由微软维护的: https://github.com/MicrosoftArchive/redis/releases

  2. pip使用国内镜像,豆瓣、清华

    pip使用国内镜像,豆瓣.清华 2017年01月18日 22:27:44 阅读数:4416 Python开发的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导 ...

  3. 记录github 免登陆用户名密码方式

    1.https 代码模式切换为ssh模式: (本博客有文章介绍) 2.~/.ssh/github_rsa.pub 内容添加到github “config“ 目录下面 3.配置~/.ssh/config ...

  4. 很有必要了解的HTML嵌套规则

    最近在重新学习HTML的知识,算是对HTML的一个重新认识吧!别小看了这东西,一切的网页可都是以它为基础的!下面就详细归纳一下HTML标签的嵌套规则吧,希望对大家有所帮助. XHTML的标签有许多:d ...

  5. C 学习之大小端

    题外话:如有误,请留言通知我. 大小端,是数值在内存中的排列方式:数值的高低位与内存地址的大小 的关系. 大/小端,英文Big/Little Endian. 注意:endian,就是字节序.字节存储顺 ...

  6. e679. 浮雕化图像

    This example demonstrates a 3x3 kernel that embosses an image. Kernel kernel = new Kernel(3, 3, new ...

  7. dm8127前段采集和抓拍

         高清监控(944275216) 2014-1-17 9:36:24自主研发高清网络摄像机,720P.960P.1080P系列产品,经济型.低照型.宽动态型等各种机型可选,支持onvif.P2 ...

  8. Erlang TCP Socket的接收进程的2种方案

    转自:http://blog.csdn.net/summerhust/article/details/8740973 一旦打开了一个使用TCP连接的套接字,它就始终保持打开状态,直至任何一方关闭它或因 ...

  9. MVC下载(存在文件的情况)

    前台: <h1>下载</h1> <button onclick="location = 'download'">下载</button> ...

  10. SpringMVC-Controller

    接上: web.xml Spring-servlet.xml Controller层是控制层,在其类上添加@Controller注解,会被Spring-servlet.xml中的<context ...