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. JVM中的垃圾回收

    GC 垃圾回收回收什么 Java的内存分配原理与C/C++不同.C/C++每次申请内存时都要malloc进行系统调用.而系统调用发生在内核空间,每次都要中断进行切换,这须要一定的开销. 而Java虚拟 ...

  2. QML vs WEB

    UI领域, 如果是桌面应用,QML可以更快速.如果是手机UI,H5绝对占优. 移动设备已经为各行业的HMI的响应性和用户友好性设定了标准.汽车,医疗设备,工业自动化系统和消费电子产品制造商现在希望为其 ...

  3. c# dataTable 合并两个列到一个新列中

    public static DataTable UniteDataTableColumns(DataTable dt, String newColumnName, string ColumnName1 ...

  4. 实现整数转化为字符串函数itoa()函数

    函数原型: char *itoa( int value, char *string,int radix);原型说明:value:欲转换的数据.string:目标字符串的地址.radix:转换后的进制数 ...

  5. tp-01 搭建过程

    1:拷贝ThinkPHP框架系统文件夹自己的www目录中的tp-shop文件夹中 2:新建自己的项目文件(比如:shop)夹与ThinkPHP框架系统文件夹在同一级目录(当然也可以不同) 3: 在tp ...

  6. android抓包工具——使用fiddler4在安卓手机抓包

    Fiddler是一款非常流行并且实用的http抓包工具,它的原理是在本机开启了一个http的代理服务器,然后它会转发所有的http请求和响应,因此,它比一般的firebug或者是chrome自带的抓包 ...

  7. CSS控制显示图片的一部分

    使用情形:防止反复请求图片资源,我们经常采用一张图片多种效果或内容显示. 假设我有纸张竖直方向的一张图片,竖直y轴方向分别是字母:A,B,C.... 现在分别要显示A.B.C 等字母,我们的CSS可以 ...

  8. Hadoop2的Yarn和MapReduce2相关

    转自: http://www.aboutyun.com/thread-7678-1-1.html..   问题导读: 1.什么是yarn? 2.Yarn 和MapReduce相比,它有什么特殊作用 ? ...

  9. mysql没有my.ini文件

    解决方法: 上面的任意一个文件拷贝一份,重命名my.ini.

  10. Ubuntu telnet

    首先在Ubuntu中安装xinetd(它是inetd替代品): sudo apt-get install xinetd 再安装telnetd,在Ubuntu中没有telnetd这个软件包,它是包含在i ...