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

题意:有n个珠子须要你染上特定的颜色。初始的时候是没有染色的。每次染的代价是不同颜色的平方。

思路:借鉴一段题解:双向链表优化dp。dp[i]表示涂完前i个所化的最小代价,显然有dp[i]=min{dp[j]+num(j+1,i)^2},当中1<=j<i,num(j+1,i)表示区间[j+1,i]的颜色个数。

这样复杂度为O(n^2)显然超时。那么须要优化一下,比方第二组測试数据3 4 2 4 4 2 4 3 2 2,如果dp[1]...dp[8]已更新完成。如今要更新dp[9],能够看到a[9]为2。依照原始的dp[i]=min{dp[j]+num(j+1,i)^2},

i=9,枚举j=8,dp[9]=min{dp[9],dp[8]+1^2};j=7,dp[9]=min{dp[9],dp[7]+2^2};如今貌似没什么变化。。。

j=6,这里就奇妙了,假设dp[9]=min{dp[9],dp[6]+3^2},那么这个就太弱了,由于此时2 4 3是连着涂的,可是2之前是3 4 2 4 4,这些假设跟着一起涂,那么仍然是3^2的代价。但前面的数字变少了。显然这样的更优。

于是乎dp[9]=min{dp[9],dp[0]+3^2},能够看到6直接跳到了0,为什么这么跳?由于这之前都是些234啊,反复了不是必需保存。

所以在dp时,我们仅仅须要维护好前后关系就可以。

比方当前dp第i位,那么即a[i]加进来,所以之前假设有a[i]值的必须删掉。详细双向链表维护。因此能够看到随意时刻。每种颜色仅仅会保存一次。复杂度就降下来了。

但仍然能够给出坑爹的数据,比方1 2 3 4 ... n那么这个dp的话,复杂度仍为O(n^2),于是继续优化。

我们知道假设一个一个涂,那么须要花费n。

所以最优方案不能大于n。也就是不能连着sqrt(n)个不同的颜色一起涂,否则代价大于n了。这里进行剪枝。复杂度降为O(nsqrt(n)),还是能够接受的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 50010;
const int inf = 0x3f3f3f3f; int a[maxn], pre[maxn], nxt[maxn], dp[maxn];
map<int, int> mp; int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pre[i] = i - 1;
nxt[i] = i + 1;
} mp.clear();
memset(dp, inf, sizeof(dp));
dp[0] = 0, pre[0] = -1;
for (int i = 1; i <= n; i++) {
if (!mp.count(a[i])) mp[a[i]] = i;
else {
int id = mp[a[i]];
nxt[pre[id]] = nxt[id];
pre[nxt[id]] = pre[id];
mp[a[i]] = i;
} int cnt = 0;
for (int j = pre[i]; j != -1; j = pre[j]) {
cnt++;
dp[i] = min(dp[i], dp[j] + cnt * cnt);
if (cnt * cnt > i)
break;
}
} printf("%d\n", dp[n]);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU - 5009 Paint Pearls(dp+优化双向链表)的更多相关文章

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

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

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

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

  3. HDU 5009 Paint Pearls (动态规划)

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

  4. hdu 5009 Paint Pearls

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

  5. hdu5009 Paint Pearls[指针优化dp]

    Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...

  7. HDOJ 5009 Paint Pearls

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

  8. AC日记——Paint Pearls hdu 5009

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

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

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

随机推荐

  1. jquery实现锚点动画效果

    锚点相信大家都使用过吧!点击后僵硬的切换是不是很不爽呢? 下面分享一个小技巧,根据锚点offset值来实现动画切换 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  2. Mysql 权限修改何时生效

    首先权限是记录在表中的,所以如果我们要修改权限只要修改表中的数据就可以了! 方法 1 grant ,revoke,set password,rename user .......等等 2 insert ...

  3. javascript集合求交集

    两集合求交集 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以 ...

  4. [LeetCode] Best Time to Buy and Sell Stock Solution

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  6. 使用LAMP创建基于wordpress的个从博客站点

    參考: http://blog.csdn.net/ck_boss/article/details/27866117 一.mysql配置 1.安装mysql yum install mysql-serv ...

  7. 本地yum源安装GCC

    Linux环境下yum源安装GCC 前提条件是有Linux环境的安装盘ISO文件 在Linux系统中创建两个目录,一个是用来存放ISO文件,一个是用来挂载该ISO文件,如下: $mkdir /root ...

  8. 用JS判断两个数字的大小

    js中的var定义的变量默认是字符串,如果单纯的比较字符串的话,会出现错误,需要先转化为int类型在做比较. [备注:110和18在你写的程序中是18大的,因为 这两个数都是字符串,而1和1相等之后比 ...

  9. 关于js封装框架类库之样式操作

    在js中,对样式的操作我们并不感到陌生,在很多框架中都是用极少的代码,实现更强大的功能,在这做出一些的总结.存在不足还望指出! 1.封装一个添加css的方法(这篇引用了前面的框架结构) 在 js 中 ...

  10. mysql 数据备份

    一.备份数据库并下载到本地[db_backup.php] php代码: <?php // 数据库参数(此处测试,直接给定,项目中使用配置文件) $cfg_dbname = 'blog'; $cf ...