HDU 5009 Paint Pearls

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 50005
int 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值