Paint Pearls

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
 

题目大意:

给定一系列的颜色。能够划分为随意多个随意大小的区间。每一个区间的花费为 区间颜色数的平方,问你总花费最小是多少?

解题思路:

用动态规划,双向链表事实上就是维护前面不同的元素,同样的元素删除。

我參照的是:http://blog.csdn.net/u011345136/article/details/39759935

解题代码:

#include <iostream>
#include <map>
#include <cstdio>
using namespace std; const int maxn=51000;
int n,d[maxn],dp[maxn],pre[maxn],next[maxn];
map <int,int> mp; //mp 记录数字相应的下标
//pre 记录前驱
//next 记录后继
void solve(){
mp.clear();
for(int i=1;i<=n;i++){
pre[i]=i-1;
next[i]=i+1;
dp[i]=(1<<30);
}
dp[0]=0;pre[0]=-1;
for(int i=1;i<=n;i++){
if(mp.find(d[i])==mp.end()) mp[d[i]]=i;
else{
int id=mp[d[i]];
next[pre[id]]=next[id];
pre[next[id]]=pre[id];
mp[d[i]]=i;
}
int c=0;
for(int j=pre[i];j!=-1;j=pre[j]){
c++;
dp[i]=min(dp[i],dp[j]+c*c);
if(c*c>=i) break;
}
}
printf("%d\n",dp[n]);
} int main(){
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&d[i]);
solve();
}
return 0;
}

HDU 5009 Paint Pearls (动态规划)的更多相关文章

  1. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

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

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

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

  3. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  4. hdu 5009 Paint Pearls

    首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...

  5. HDOJ 5009 Paint Pearls

    Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...

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

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

  7. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

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

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

  9. Paint Pearls

    Paint Pearls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009 dp+双向链表优化 看到题目,很自然地可以定义状态:dp[i]表示涂好 ...

随机推荐

  1. 项目.c文件和.h文件关系

                "如有不正确之处,请指出,谢谢"       --Mood 对于一个项目,我们应该要很好的处理众多的.c和.h文件. 1.通过头文件调用库功能:#inclu ...

  2. C语言中的enum(枚举)使用方法

    近期在写数据结构的广义表时候用到了这个概念,在学习C语言的时候没有太注意们这里学一下. 我在网上结合了非常多资料,这里自己总结一下. 首先说.JAVA和C++中都有枚举类型. 假设一个变量你须要几种可 ...

  3. Eclipse选项卡式的属性视图(The Eclipse Tabbed Properties View)

    Eclipse工作台提供了一个性能视图用于查看(和/或编辑)选定项目的属性. 在本文中,您将学习怎样使用选项卡式的属性视图创建一个性能增强的用户界面视图. 1.引言 Eclipse工作台提供了一个属性 ...

  4. js数组基础整理

    首页: 主要整理了一下数组中常用的一些基础知识,代码都是自己手敲,有不对的地方希望能指出,目前只有4篇,后续会不断的增加这一板块. 由于少于100字不能发所以把一些最基本的创建数组也写上. // 创建 ...

  5. OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)

    这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...

  6. spring Jdbc自己主动获取主键。

    学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了 package com.power.dao; import java.lang.reflect.Paramet ...

  7. Kendo UI开发教程(26): 单页面应用(四) Layout

    Layout继承自View,可以用来包含其它的View或是Layout.下面例子使用Layout来显示一个View 1 <div id="app"></div&g ...

  8. HDU2504 又见GCD

    又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  9. windows下RabbitMQ 监控

    RabbitMQ的监控很简单,网上也有很多资料,但是大都不详细,让人云里雾里,我这里详细总结下. RabbitMQ本身提供了一个web的监控页面,只需要简单的几部命令行就可以访问这个页面了. 1.打开 ...

  10. Shell echo命令

    Shell echo命令 echo "It is a test" 这里的双引号完全可以省略 .显示变量 read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shel ...