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. DOM方法入门 - 第二课

    1.console.log()是吧元素显示在控制台2.appendChild() 就是将元素追加到末尾3.innerHTML获取的是元素里面的所有内容包括里面的子元素4.innerText获取的是元素 ...

  2. sn9c291 驱动载入成功,mpayer无法播放

    先眼下将一个sn9c291+ov9712的模块驱动在fedora上载入成功,但是在使用mplayer却无法播放,不知道为何? watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  3. SpringMVC返回json数据的三种方式

    1.第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew. 如:<bean id="defaultJsonView" cla ...

  4. Mockito文档-单元测试技术

    Overview  Package   Class  Use  Tree  Deprecated  Index  Help     PREV CLASS   NEXT CLASS FRAMES     ...

  5. eclipse升级后Android使用JAR报错

    升级ADT22以后,老项目编译时后遇到 NoDefFoundClassError  这个错误,因为项目中使用了jar文件. 遇到此问题的解决步骤: 1.项目根目录下建立 libs ,并将jar文件移入 ...

  6. Sublime Text3 + Golang搭建开发环境

    Sublime Text3 + Golang搭建开发环境 http://blog.csdn.net/aqiang912/article/details/46775409 1.安装git 因为golan ...

  7. CentOS6.4 安装Mysql

    虽说,新版的数据包可能会带上一些新特性,但是数据库对我而言,还是稳定版优先.因为新特性不一定我会用到.. 下载安装: yum list | grep mysql 因为是准备搞开发用的,所以只要安装my ...

  8. SDUT Fermat’s Chirstmas Theorem(素数筛)

    Fermat's Chirstmas Theorem Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 In a letter ...

  9. UVa11183 Teen Girl Squad, 最小树形图,朱刘算法

    Teen Girl Squad  Input: Standard Input Output: Standard Output You are part of a group of n teenage ...

  10. [置顶] Embedded Server:像写main函数一样写Web Server

    1.传统的JEE Web Server 传统的JEE中,如果我们想要部署一个Web Application,我们需要首先安装一个Container Server,如JBoss,WebLogic,Tom ...