HDOJ 5009 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 k 2 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×10 4), indicating the number of pearls. The second line contains a 1,a 2,...,a n (1 ≤ a i≤ 10 9) 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 设f[i]为以i结尾的前i个数划分的序列的最小值,不难得到递推式:
f[i]=min{f[j]+cost(j+1,i)}。 朴素的话复杂度是O(N^2),过不了,要考虑优化。 然后发现其实答案有一个上界N,因为一个一个消总能做到N划分整个序列。
又因为cost是颜色数的平方,所以当i固定的时候,cost(j+1,i)最多只有sqrt(N)
个取值,要不然肯定不是最优解。 而当cost(j+1,i)一定的时候j一定是越小越好,因为f函数肯定是单调不降的(考虑反证法)。 所以我们只需要找出i一定的时候,对应的最多sqrt(N)个j的位置就行了。 但其实并不是很好求。。。。。 考虑从i-1的sqrt(N)个j来递推i的。
当i的颜色没有在i-1的j中出现的时候,显然只需要取i-1的最近的sqrt(N)-1个j再加上i作为最后一个j。
设k为i-1的第二远的j,那么a[k-1]就是最远出现的颜色,把它还原即可; 而如果出现过的话,颜色集合是不变的,唯有某一个位置换成了i。
记以i-1为右端点的时候a[i]第一次出现的位置是k,那么i-1集合中某个j肯定为k+1,
把这个j换成i就行了。
至于原因??
因为没有a[i]的时候扫到k时颜色数会+1,所以j变成了一个左端点;
而有a[i]的时候扫到k时颜色数就不会变了。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#define pb push_back
#define ll long long
#define maxn 100005
using namespace std;
//g[i][j]表示右端点为i的有j+1个颜色的块的可能的最左端
vector<int> g[maxn];
using namespace std;
const int inf=<<;
int n,a[maxn],f[maxn],b[maxn],ky;
bool v[maxn];
int main(){
while(scanf("%d",&n)==){
memset(v,,sizeof(v));
for(int i=;i<=n;i++) g[i].clear(); for(int i=;i<=n;i++){
scanf("%d",a+i);
//先把重复的去了,没有用
if(a[i]==a[i-]) i--,n--;
}
//离散化一下,不然会出事
for(int i=;i<=n;i++) b[i]=a[i];
sort(b+,b+n+);
ky=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++) a[i]=lower_bound(b+,b+ky+,a[i])-b; //颜色数上界,超过上界的cost肯定不在最优解中
int tp=sqrt(n); g[].pb(),v[a[]]=;
for(int i=;i<=n;i++){
if(v[a[i]]){
/*
如果当前颜色在前sqrt(n)种之内,
那么第一种的最左端肯定是i,
第二种往后的可以根据i-1的递推,
*/
g[i].pb(i);
int oo=g[i-].size()-;
for(int j=;j<=oo;j++){
if(a[g[i-][j]-]==a[i]) continue;
g[i].pb(g[i-][j]);
}
}
else{
/*
不在之内的话,就得新加进去然后
把上一个的最末尾的颜色消除
*/
v[a[i]]=,g[i].pb(i);
int m=min((int)g[i-].size(),tp-);
for(int j=;j<=m;j++) g[i].pb(g[i-][j-]); if(m<g[i-].size()){
v[a[g[i][m]-]]=;
}
}
/*
printf("%d:%d\n",i,g[i].size());
for(int j=0;j<g[i].size();j++) printf("%d ",g[i][j]);
puts("");
*/
} f[]=; for(int i=;i<=n;i++){
int cost=,sz=g[i].size()-;
f[i]=inf; for(int j=;j<=sz;j++){
cost++;
if(cost*cost>f[i]) continue; f[i]=min(f[i],f[g[i][j]-]+cost*cost);
}
} printf("%d\n",f[n]);
} return ;
}
HDOJ 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 (动态规划)
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- 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个节 ...
- 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]表示涂好 ...
- hdu5009 Paint Pearls[指针优化dp]
Paint Pearls Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
随机推荐
- 【bzoj3224】Tyvj 1728 普通平衡树 01Trie姿势+平衡树的四种姿势 :splay,旋转Treap,非旋转Treap,替罪羊树
直接上代码 正所谓 人傻自带大常数 平衡树的几种姿势: AVL Red&Black_Tree 码量爆炸,不常用:SBT 出于各种原因,不常用. 常用: Treap 旋转 基于旋转操作和随机数 ...
- 用PHP迭代器来实现一个斐波纳契数列
斐波纳契数列通常做法是用递归实现,当然还有其它的方法.这里现学现卖,用PHP的迭代器来实现一个斐波纳契数列,几乎没有什么难度,只是把类里的next()方法重写了一次.注释已经写到代码中,也是相当好理解 ...
- 获取html元素内容
html: <!DOCTYPE ><html> <head> <meta http-equiv="Content-Type" conten ...
- im4java学习----查看文档和test用例
im4java下载地址:http://sourceforge.net/projects/im4java/files/(谷歌搜索出来的第一个官方地址打不开) 我们需要下载bin和src 这2个压缩包. ...
- C# 序列化理解 2(转)
一.概述 序列化是把对象转变成流.相反的过程就是反序列化. 哪些场合用到这项技术呢? 1. 把对象保存到本地,下次运行程序时恢复这个对象. 2. 把对象传送到网络的另一台终端上,然后在此终端还原这个对 ...
- 九大排序算法Java实现
之前学习数据结构与算法时花了三天时间整理九大排序算法,并采用Java语言来实现,今天第一次写博客,刚好可以把这些东西从总结的文档中拿出来与大家分享一下,同时作为自己以后的备忘录. 1.排序算法时间复杂 ...
- TDS开启log TDS开启SSL
参考: http://www.ibm.com/developerworks/tivoli/library/t-tds-perf/ 1. 编辑ldif文件如下 dn: cn=Audit, cn=Log ...
- 【bzoj1911-[Apio2010]特别行动队】斜率优化
[题目描述] 有n个数,分成连续的若干段,每段的分数为a*x^2+b*x+c(a,b,c是给出的常数),其中x为该段的各个数的和.求如何分才能使得各个段的分数的总和最大. [输入格式] 第1行:1个 ...
- usaco 月赛 2005 january watchcow
2013-09-18 08:13 //By BLADEVIL var n, m :longint; pre, other :..] of longint; last :..] of longint; ...
- (十一)__LINE__、__FUNCTION__的使用
单片机中也可以用__LINE和__FUNCTION__进行异常信息打印,分别代表当前代码行数和当前代码函数名 printf("line:%d\r\n",__LINE__); pri ...