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
 

题意:

  给你一个数组,每个值代表一种颜色,每次选一个区间涂颜色,代价是区间内颜色种类数的平方,涂完所有数组,问你最小代价是多少

题解:

  设定dp[i]为前i个数的最小代价,

  那么转移就是dp[i] = min{dp[j]+cal(j+1,i)^2} cal计算区间内颜色种类数

  明显超时;

  当你从i-1遍历到0去寻找那个最小dp[j]+cal(j+1,i)^2时,有些电视可以跳跃的,那就是在k~i-1里面出现过的,就可以跳过,这个用双向链表实现

  还有一个优化:当向前遍历时,不同个数的平方已经超过单独涂色的值 即 cal(j+1,i)^2>i 直接跳出

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int N = 1e5+, M = 2e2+, inf = 2e9, mod = 1e9+;
int dp[N],a[N],pre[N],nex[N],n;
map<int,int >mp; int main()
{
while(scanf("%d",&n)!=EOF)
{
mp.clear();
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)nex[i]=i+,pre[i]=i-;
dp[]=,pre[]=-;
for(int i=;i<=n;i++)
{
if(!mp[a[i]]) mp[a[i]]=i;
else
{
int id = mp[a[i]];
nex[pre[id]] = nex[id];
pre[nex[id]] = pre[id];
mp[a[id]] = i;
}
int num = ;
for(int j=pre[i];j!=-;j=pre[j])
{
num++;
dp[i] = min(dp[i],dp[j]+num*num);
if(num*num>i) break;
}
}
printf("%d\n",dp[n]);
}
return ;
}

HDU 5009 Paint Pearls 双向链表优化DP的更多相关文章

  1. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  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. hdu5009 Paint Pearls[指针优化dp]

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

  5. hdu 5009 Paint Pearls

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

  6. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  7. poj 1260 Pearls 斜率优化dp

    这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...

  8. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  9. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

随机推荐

  1. motto6

    当你发现自己不错的时候,要适当的给自己加点凉水,让自己知道自己离“不错”还很远.

  2. bootshrap会改变IE浏览器滚动条样式

    在某个小网站的开发中 客户一直抱怨在IE11中网页右边滚动条不一样 后来发现在IE11中,有2个页面滚动条会自动隐藏,一开始以为是浏览器默认行为,改了overflow:scroll后也没有用.仔细观察 ...

  3. app内嵌web的一些问题记录

    问题(1)webview里面出现大图预览,点击手机上的返回,应该是图片预览消失 问题(2)键盘输入的时候,键盘会把输入框遮挡 ------------------------------------- ...

  4. 昂贵的聘礼(dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38549   Accepted: 11158 Descripti ...

  5. ajax 之js读取xml的多浏览器兼容

    主要是分为两大类:IE.其它浏览器 IE8以下只支持这种 InputVoltage.innerText = xmlDoc.getElementsByTagName(id)[0].text, 其它浏览器 ...

  6. table表头标题th浮动提示-MyTable.js

    /* $(document).ready(function () { var maxH = ($(window).height() - $("#divParent").positi ...

  7. mysql启动报错(mac)

    $mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) ...

  8. ZeroMQ(java)之负载均衡

    我们在实际的应用中最常遇到的场景如下: A向B发送请求,B向A返回结果.... 但是这种场景就会很容易变成这个样子: 很多A向B发送请求,所以B要不断的处理这些请求,所以就会很容易想到对B进行扩展,由 ...

  9. 让Linux下的打印机hp1020、hp p1008自动加载固件

    前言: 前段时间,处理公司打印机服务器Linux化工作.遇到问题如下:hp1020.hp1008断电后不能继续打印.而其他打印机在连接Linux打印机的情况下,断电后也能正常打印. 鉴于此情况,我搜寻 ...

  10. 关于LINUX文件与目录的问题说明

    文件权限一般可认为是0 123 456 789,一共十位: 0:表示该文件的文件类型.Windows里面是使用了一种文件关联的技术,通过扩展名来关联相应的应用程序,使得双击某个文件,就能达到调用相应的 ...