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. jquery子元素过滤选择器

    :nth-child('索引值')//获取指定元素下的某个子元素的位置,索引从1开始: //偶数行                 //$('li:nth-child(even)').addClass ...

  2. C++操作符的优先级

    C++操作符的优先级 C++操作符的优先级 操作符及其结合性 功能 用法 L L L :: :: :: 全局作用域 类作用域 名字空间作用域 ::name class::name namespace: ...

  3. 基于visual Studio2013解决面试题之0601二叉树深度

     题目

  4. checkbox之checked的方法(attr和prop)区别

    1. $('#checkbox').click(function(){ if($('#checkbox').is(':checked')) { $(".sendmailhui"). ...

  5. hibernate简单介绍

    1.   Hibernate是什么? hibernate是 轻量级的 ORM 框架. ORM全称object/relationmapping [对象/关系映射]. Hibernate主要用来实现Jav ...

  6. Android 事件处理

    目的:通过全面的分析Android的鼠标和键盘事件.了解Android中如何接收和处理键盘和鼠标事件,以及如何用代码来产生事件. 主要学习内容: 1. 接收并处理鼠标事件:按下.弹起.移动.双击.长按 ...

  7. 快速排序原理、复杂度分析及C语言实现

    本文作者华科小涛:@http://www.cnblogs.com/hust-ghtao/,参考<算法导论>,代码借用<剑指offer> 快速排序是一种最坏情况时间复杂度为的排序 ...

  8. robotframework ride 版本兼容问题

    在安装robotFramework ride的时候,必须需要使用wxpython 目前使用的wxpython 还必须是unicode 版本的要不然不支持中文 目前使用的 wx.version.2.8. ...

  9. jyphon 环境变量配置

    Jyphon 是基于java平台python 的一种实现 官网: http://www.jython.org/ 可以从官网下载 jyphon 安装 下载 jython Installer ,下载之后是 ...

  10. vim 操作指令2

    VIM命令大全 光标控制命令 命令 光标移动 h 向左移一个字符 j 向下移一行 k 向上移一行 l 向右移一个字符 G 移到文件的最后一行 w 移到下一个字的开头 W 移到下一个字的开头,忽略标点符 ...