hdu5009 Paint Pearls 西安网络赛C题
来源:互联网 发布:一入淘宝深似海下一句 编辑:程序博客网 时间:2024/05/16 12:29
Paint Pearls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2018 Accepted Submission(s): 662
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 totheir 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 totheir 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
对一段区间的花费是这个区间不同数个数的平方,求整个区间的最小花费。
第一眼看上去是很简单的dp。。但是卡时间。。。
有一种类似链表的做法,在扫一遍的过程中记录每种颜色最后一次出现的位置,链表直接跳到出现下一种没出现过颜色的位置,不用一个一个找。具体做法是加入当前颜色c,若c上次出现的位置是p[c],那么把l[p[c]]和r[p[c]]连接,也就是说跳过p[c]。更新某个点dp的时候,就沿着左链表的路径更新,每走一步都多了一种颜色。
因为前i个数最多花费是i,所以如果不同颜色种数cnt的平方大于i了就不用再继续往下更新了。
#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>#define INF 0x3f3f3f3f#define eps 1e-9#define MAXN 50010#define MAXM 60#define MAXNODE 105#define MOD 100000#define SIGMA_SIZE 4typedef long long LL;using namespace std;int N,dp[MAXN],l[MAXN],r[MAXN],p[MAXN],a[MAXN];map<int,int> mp;int main(){ freopen("in.txt","r",stdin); while(scanf("%d",&N)!=EOF){ mp.clear(); int n=0; memset(l,-1,sizeof(l)); for(int i=0;i<=N;i++) dp[i]=i; for(int i=1;i<=N;i++){ scanf("%d",&a[i]); if(mp[a[i]]==0) mp[a[i]]=++n; else{ int tmp=p[mp[a[i]]]; r[l[tmp]]=r[tmp]; l[r[tmp]]=l[tmp]; } if(i==1||a[i]!=a[i-1]) l[i]=i-1; r[i]=i+1; p[mp[a[i]]]=i; int cnt=0; for(int j=l[i];j!=-1;j=l[j]){ cnt++; dp[i]=min(dp[i],dp[j]+cnt*cnt); if(cnt*cnt>i) break; } } printf("%d\n",dp[N]); } return 0;}
0 0
- hdu5009 Paint Pearls 西安网络赛C题
- HDU 5009 Paint Pearls(西安网络赛C题)
- hdu5009 Paint Pearls DP
- DP - hdu5009 Paint Pearls
- hdu5009 Paint Pearls(DP+STL优化)
- hdu5009 Paint Pearls(离散+线性dp)
- HDU5009西安网赛2014
- 2014西安网络预选赛1003(DP+剪枝)hdu5009
- 西安网络赛e题
- 【2017西安网络赛】C Sum
- HDOJ 5009 Paint Pearls
- hdu 5009 Paint Pearls
- hdu Paint Pearls 5009
- hdu 5009 Paint Pearls
- hdu 5009 Paint Pearls
- hdu 5009 Paint Pearls
- HDU 5009 Paint Pearls
- HDU 5009 Paint Pearls
- uva 724 Prime Ring Problem
- 快播网站最后,她打开厢房的窗户,把一盆浊水给倒进湖里
- 马尔科夫随机场
- 推荐一个学通信技术的网站,百度“51学通信”第一个就是
- My Solution: Word Ladder
- hdu5009 Paint Pearls 西安网络赛C题
- atitit.组件化事件化的编程模型--服务端控件(1)---------服务端控件与标签的关系
- 【约瑟夫】编程实现约瑟夫环
- 基于HibernateTemplate的dao和service文件的自动生成
- python文件夹遍历,文件操作,获取文件修改创建时间
- Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结
- Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9
- C语言中struct和union的区别是什么
- Atitit.javascript 实现类的方式原理大总结