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. ios两视图间托付(delegate)传值

    现有两个视图(ViewController.ViewController1),从ViewController中带參数跳转到ViewController1,在ViewController1选中数据后带有 ...

  2. 怎样统计分析CSDN博客流量

    第一.IP.PV和UV各自是什么意思? IP.实际上也就是指独立IP,它的英文为Internet ***otocol,是独立IP数的意思.00:00-24:00同样IP地址记录一次.即使你有多台电脑. ...

  3. 【RMAN】使用RMAN增量备份刷新 Standby Database

    Step 1: Create the Incremental Backup RMAN> BACKUP DEVICE TYPE DISK INCREMENTAL FROM SCN 750983 D ...

  4. 结合使用 Oracle 和 Ruby on Rails 的补充

    本文是对此文的补充: 结合使用 Oracle 和 Ruby on Rails http://www.oracle.com/technetwork/cn/tutorials/rubyrails-0959 ...

  5. Windows上建立、取消共享文件夹

    建立共享文件夹 1.创建一个文件夹test 2.右键属性,点击共享 4.在另外一台机器上访问该共享文件 取消共享文件夹 右键属性,点击高级共享

  6. ffplay(2.0.1)中的音视频同步

    最近在看ffmpeg相关的一些东西,以及一些播放器相关资料和代码. 然后对于ffmpeg-2.0.1版本下的ffplay进行了大概的代码阅读,其中这里把里面的音视频同步,按个人的理解,暂时在这里作个笔 ...

  7. QListView的子项的ViewMode

    QListView.setViewMode(ViewMode mode) enum QListView::ViewMode Constant    Value    DescriptionQListV ...

  8. 接口日志记录AOP实现-LogAspect

    使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...

  9. iOS: Assertion failure on picker view

    Q:I'm getting an assertion failure while scrolling a picker view w/ zero data(zero rows). While scro ...

  10. jquery注册文本框获取焦点清空,失去焦点赋值的简单实例

    在我们开发过程中特别是用户注册时会有一个效果,就是文本框获取焦点清空提示,如果用户没有输入信息失去焦点赋值上我们的提示语.   <html> <head> <meta h ...