hdu 5009 Paint Pearls 2014 ACM/ICPC Asia Regional Xi'an Online
来源:互联网 发布:js window发送url 编辑:程序博客网 时间:2024/05/01 11:46
Paint Pearls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1014 Accepted Submission(s): 325
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
Source
2014 ACM/ICPC Asia Regional Xi'an Online
Recommend
hujie | We have carefully selected several similar problems for you: 5017 5016 5015 5014 5013
题意:区间涂色,花费为颜色数的平方,求最小的花费。
思路:思路直接看代码。比赛的时候想到过dp,但是o(n^2)的算法必定超时 ,就没有做,比完看居然n^2的算法加剪枝就可以过掉。g++1.4s
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N = 50005;const int inf=0x3f3f3f3f;struct node{ int id,c,l;}t[N];int c,n,cnt;int vis[N],dp[N];vector<int> v;bool cmp1(node a,node b){ return a.c<b.c;}bool cmp2(node a,node b){ return a.id<b.id;}void read_int(int&x){ char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); x = ch - '0'; ch=getchar(); while('0'<=ch&&ch<='9') { x = 10*x + ch-'0'; ch = getchar(); }}int main(){ while(scanf("%d", &n)!=EOF){ read_int(t[1].c); int id=1; t[1].id=1; for(int i=2;i<=n;i++){ read_int(c); if(c!=t[id].c){ t[++id].c=c; t[id].id=id; } } n=id; sort(t+1,t+n+1,cmp1); id=0; t[1].l=0; for(int i=2;i<=n;i++){ if(t[i].c!=t[i-1].c)id++; t[i].l=id; } sort(t+1,t+n+1,cmp2); memset(dp,inf,sizeof(dp)); memset(vis,0,sizeof(vis)); dp[0]=0; dp[n]=n; for(int i=0;i<n;i++){ cnt=0; if(dp[i]>=dp[i+1])continue; for(int j=i+1;j<=n;j++){ if(!vis[t[j].l]){ v.push_back(t[j].l); vis[t[j].l]++; cnt++; } if(dp[i]+cnt*cnt>=dp[n])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[n]); } return 0;}
0 0
- hdu 5009 Paint Pearls 2014 ACM/ICPC Asia Regional Xi'an Online
- hdu 5009 Paint Pearls 2014 ACM/ICPC Asia Regional Xi'an Online
- HDU 5009 Paint Pearls _(:зゝ∠)_2014 ACM/ICPC Asia Regional Xi'an Online
- HDU 5009 DP (2014 ACM/ICPC Asia Regional Xi'an Online)题解
- 2014 ACM/ICPC Asia Regional Xi'an Online 小记
- 2014 ACM/ICPC Asia Regional Xi'an Online
- 2014 ACM/ICPC Asia Regional Xi'an Online(hdu 5007 - hdu 5017)
- hdu 5015 233 Matrix 2014 ACM/ICPC Asia Regional Xi'an Online 矩阵快速幂
- hdu 5012 Dice 2014 ACM/ICPC Asia Regional Xi'an Online bfs
- hdu 5014 Number Sequence 2014 ACM/ICPC Asia Regional Xi'an Online 数论
- HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi'an Online
- hdu 1009 233 Matrix 矩阵构造 --2014 ACM/ICPC Asia Regional Xi'an Online
- HDU 5015 233 Matrix / 2014 ACM/ICPC Asia Regional Xi'an Online
- hdu 5014 Number Sequence 找规律 | 贪心 2014 ACM/ICPC Asia Regional Xi'an Online
- hdu 5008(2014 ACM/ICPC Asia Regional Xi'an Online ) Boring String Problem(后缀数组&二分)
- hdu 5011 Game Nim博弈 2014 ACM/ICPC Asia Regional Xi'an Online
- hdu 5007 Post Robot 水题 2014 ACM/ICPC Asia Regional Xi'an Online
- 2014 ACM/ICPC Asia Regional Xi'an Online 233 Matrix,hdu 5015
- Python学习笔记(五)Python动态类型和引用
- jquery源码
- Oracle SQL性能优化
- 反射配对儿XML
- opengl纹理
- hdu 5009 Paint Pearls 2014 ACM/ICPC Asia Regional Xi'an Online
- 数据库设计三范式
- RecyclerView
- 【最大流】POJ-1273 Drainage Ditches
- Leetcode【二叉树】:Binary Tree Postorder Traversal 题解(非递归解法),可以说是【二叉树】入门必须掌握
- HDU-5014 Number Sequence 最大异或和
- HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(博弈论)
- Java transient关键字
- [iOS] 初探 iOS8 中的 Size Class