HDU 5009 Paint Pearls (动态规划)
Paint Pearls
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.
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.
3
1 3 3
10
3 4 2 4 4 2 4 3 2 2
2
7
题目大意:
给定一系列的颜色。能够划分为随意多个随意大小的区间。每一个区间的花费为 区间颜色数的平方,问你总花费最小是多少?
解题思路:
用动态规划,双向链表事实上就是维护前面不同的元素,同样的元素删除。
我參照的是: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 (动态规划)的更多相关文章
- HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- hdu 5009 Paint Pearls
首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...
- HDOJ 5009 Paint Pearls
Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...
- HDU-5009 Paint Pearls 动态规划 双向链表
题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
- Paint Pearls
Paint Pearls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009 dp+双向链表优化 看到题目,很自然地可以定义状态:dp[i]表示涂好 ...
随机推荐
- sn9c291 驱动载入成功,mpayer无法播放
先眼下将一个sn9c291+ov9712的模块驱动在fedora上载入成功,但是在使用mplayer却无法播放,不知道为何? watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- [置顶] 程序员面试之道(《程序员面试笔试宝典》)之看着别人手拿大把的offer,不淡定了怎么办?
不管是在哪里,不管发生什么事,不要随便放下自己. ——<当男人恋爱时> 很多求职者都会面临一个问题:别人手拿大把大把的offer了,而自己却是两手空空,别人签约之后已经过着“猪狗不如”的悠 ...
- 杂题_POJ上的过桥问题
本文出自:http://blog.csdn.net/svitter 过桥问题解释:一条船能够坐两个人,可是有非常多人要过河,所以送过一个人去,还有一个人还要回来接.使全部人过河之后时间最短,怎样求? ...
- ASP.NET - 后台获取按钮绑定的值CommandArgument
<asp:LinkButton runat="server" ID="resumelbtn" CommandArgument='<%# Eval(& ...
- 用Delphi实现Windows的鼠标钩子函数
Delphi是基于PASCAL语言的Windows编程工具,功能十分强大.然而在Delphi的帮助文件中,对Windows API函数的说明沿袭了 VC 的格式,和VC一样,对很多API函数的用法没有 ...
- 拷贝构造函数和const成员函数
实验原因 说明如何使用const描述保护类数据不会意外修改. 编译环境 vc6sp6 + win7x64 工程下载 copyConstruction_constMemberFunction.zip ...
- 正则表达式概述与JAVA中正则表达式的应用
编程或者电脑使用过程中,经常需要对字符串进行 匹配,查找,替换,判断.如果单纯用代码 if () ,whlie 什么的进行比较复杂麻烦.正则表达式是一种强大灵活的文本处理工具,专门对字符串进行匹配,查 ...
- windows的定时任务设置
windows 的Schedule Task .创建配置 1.点击"開始" 2.点击"控制面板" 3.双击"任务计划" 4.双击" ...
- Computational Geometry Template
顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...
- HDU 2098 分拆素数和(素数)
HDU 2098 分拆素数和(素数) http://acm.hdu.edu.cn/showproblem.php?pid=2098 题意: 给你一个偶数,问你这个偶数有多少种方式能由两个不同的素数构成 ...