hdu 5009 dp+离散化
来源:互联网 发布:微信矩阵管理平台 编辑:程序博客网 时间:2024/06/06 04:01
http://acm.hdu.edu.cn/showproblem.php?pid=5009
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.
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.
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
31 3 3103 4 2 4 4 2 4 3 2 2
Sample Output
27
/**hdu 5009 dp+离散化题目大意:为给定的一个区间的各个位置染色,已知要求的各个位置的目标颜色,每次染色的区间为连续的,可一次性染成目标颜色,每次的花费为这个区间上不同颜色的种类的平方,求最小花费解题思路:定义dp[i]表示前i个染完色的最小花费。首先把连续的相同的数去掉,由于颜色1~1e9过于分散,离散化到1~n之间然后转移即可。有一个剪枝,见代码注释*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>using namespace std;const int N=50004;int dp[N],a[N],vis[N];int n;struct note{ int num,ip,rank;}kk[N];int cmp1(note a,note b){ return a.num<b.num;}int cmp2(note a,note b){ return a.ip<b.ip;}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); int k=1; kk[1].num=a[1]; kk[1].ip=1; for(int i=2;i<=n;i++) { if(a[i]!=a[i-1]) { kk[++k].num=a[i]; kk[k].ip=k; } } sort(kk+1,kk+k+1,cmp1);///离散化============================= int cnt=2; kk[1].rank=1; for(int i=2;i<=k;i++) { if(kk[i].num!=kk[i-1].num) kk[i].rank=cnt++; else kk[i].rank=kk[i-1].rank; }///=================================== sort(kk+1,kk+k+1,cmp2); for(int i=0;i<50004;i++) dp[i]=0xfffffff; memset(vis,0,sizeof(vis)); dp[0]=0;//这里是关键 dp[k]=k; vector<int>v; for(int i=0;i<k;i++) { cnt=0; for(int j=i+1;j<=k;j++) { if(!vis[kk[j].rank]) { cnt++; v.push_back(kk[j].rank); vis[kk[j].rank]=1; } if(dp[i]+cnt*cnt>=dp[k])//剪枝 break; dp[j]=min(dp[j],dp[i]+cnt*cnt); } for(int j=0;j<v.size();j++) vis[v[j]]=0; v.clear(); } printf("%d\n",dp[k]); } return 0;}/**31 3 3103 4 2 4 4 2 4 3 2 251 2 3 4 551 1 1 1 151 2 2 4 12 7 5 1 4*/
0 0
- hdu 5009 dp+离散化
- 【dp+离散化】hdu 4028
- hdu 5009 离散化+链表+dp
- HDU 3607 线段树+离散化+DP
- hdu 3616 Milking Time dp 离散化
- 【树状数组 + 离散化 + DP】 HDU 5542
- hdu 5009 离散化
- HDU 3450 Counting Sequences(树状数组+DP+离散化)
- hdu 3450 离散化+dp+线段树优化
- HDU 2836 Traversal(线段树+离散化+DP)
- HDU 5542 ccpc 树状数组优化dp +离散化
- hdu 4028 The time of a day(离散化dp)
- HDU 3016 Man Down(线段树+离散化+dp)
- hdu3666 离散化dp
- Millionare_离散化&&DP
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- HDU 1556 离散化
- hdu 5481 离散化
- java.security.MessageDigest
- POJ 1724 Corporate Identity 后缀数组
- 解决Xming字体太小的问题
- 对以后有点想法了
- STM32 SD bootloader实现
- hdu 5009 dp+离散化
- JAVA笔试题
- KNN算法图解(MATLAB实例)
- linux awk
- 博客的开始
- mysql innodb master_thread伪代码整理
- 第11周项目六 回文~素数(1)
- SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
- 最近生活