HDU 5009 Paint Pearls

来源:互联网 发布:zara淘宝 编辑:程序博客网 时间:2024/05/19 11:37

Paint Pearls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3246 Accepted Submission(s): 1058
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
Source
2014 ACM/ICPC Asia Regional Xi’an Online
Recommend
hujie | We have carefully selected several similar problems for you: 6022 6021 6020 6019 6018

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<map>using namespace std;map<int,int>mp;#define maxn 50005int l[maxn],r[maxn],f[maxn],n,a[maxn],p[maxn],c,num,m;int main(){    while(scanf("%d",&n)!=EOF){        m=0;// 统计不同颜色数目        for(int i=1;i<=n;i++){            scanf("%d",&c);            if(i==0) a[++m]=c;            else if(c!=a[m]) a[++m]=c;        }        for(int i=0;i<=m;i++) f[i]=i;        r[0]=1; num=0;        mp.clear();        for(int i=1;i<=m;i++){            if(!mp[a[i]]) mp[a[i]] = ++num;            else{                int temp=p[mp[a[i]]];                r[l[temp]]=r[temp];                l[r[temp]]=l[temp];            }            r[i]=i+1;l[i]=i-1;p[mp[a[i]]]=i;            int cnt=0;            for(int j=l[i];;j=l[j]){                cnt++;                f[i]=min(f[i],f[j]+(cnt*cnt));                if(j==0||(cnt*cnt)>i) break;            }        }        printf("%d\n",f[m]);    }    return 0;}
0 0
原创粉丝点击