博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5009 Paint Pearls (动态规划)
阅读量:5275 次
发布时间:2019-06-14

本文共 2021 字,大约阅读时间需要 6 分钟。

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 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
 

Source
 

Recommend
hujie
 

题目大意:

           给定一系列的颜色。能够划分为随意多个随意大小的区间。每一个区间的花费为 区间颜色数的平方,问你总花费最小是多少?

解题思路:

         用动态规划,双向链表事实上就是维护前面不同的元素,同样的元素删除。

        我參照的是:

解题代码:

#include 
#include
#include
using namespace std;const int maxn=51000;int n,d[maxn],dp[maxn],pre[maxn],next[maxn];map
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;}

转载于:https://www.cnblogs.com/mfrbuaa/p/5095919.html

你可能感兴趣的文章
使用js实现瀑布流
查看>>
easyui
查看>>
linux查找文件find
查看>>
BZOJ 2007: [Noi2010]海拔
查看>>
SpringMVC和mybatis的框架
查看>>
如何深拷贝一个对象数组?
查看>>
CC2538相关资料
查看>>
用js进行日期的加减
查看>>
悟空crm-0.5.4 (OpenLogic CentOS7.2)
查看>>
C++入门经典-例7.8-const对象,标准尺寸
查看>>
BZOJ 1088 扫雷
查看>>
冒泡排序
查看>>
Sharepoint 2010 用VS定制Master,并且每个Web应用同一个Master
查看>>
关于photoswiper展示时图片自适应的问题
查看>>
微信公众开发api接口
查看>>
RPC
查看>>
C#读取Mysql blob字段 分类: .NET ...
查看>>
2018 hncpc 部分题
查看>>
滚动到顶部
查看>>
python flask 学习与实战
查看>>