zoukankan      html  css  js  c++  java
  • HDU 5009 Paint Pearls

    Paint Pearls

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 3245    Accepted Submission(s): 1057

    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
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<map>
     6 using namespace std;
     7 map<int,int>mp;
     8 #define maxn 50005
     9 int l[maxn],r[maxn],f[maxn],n,a[maxn],p[maxn],c,num,m;
    10 int main(){
    11     while(scanf("%d",&n)!=EOF){
    12         m=0;// 统计不同颜色数目
    13         for(int i=1;i<=n;i++){
    14             scanf("%d",&c);
    15             if(i==0) a[++m]=c;
    16             else if(c!=a[m]) a[++m]=c;
    17         }
    18         for(int i=0;i<=m;i++) f[i]=i;
    19         r[0]=1; num=0;
    20         mp.clear();
    21         for(int i=1;i<=m;i++){
    22             if(!mp[a[i]]) mp[a[i]] = ++num;
    23             else{
    24                 int temp=p[mp[a[i]]];
    25                 r[l[temp]]=r[temp];
    26                 l[r[temp]]=l[temp];
    27             }
    28             r[i]=i+1;l[i]=i-1;p[mp[a[i]]]=i;
    29             int cnt=0;
    30             for(int j=l[i];;j=l[j]){
    31                 cnt++;
    32                 f[i]=min(f[i],f[j]+(cnt*cnt));
    33                 if(j==0||(cnt*cnt)>i) break;
    34             }
    35         }
    36         printf("%d
    ",f[m]);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    USACO Name That Number
    USACO Milking Cows
    hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
    Contest 1
    JNU周练1026
    树形DP
    Python和C扩展实现方法
    Python模拟C++输出流
    SkipList算法实现
    Python 迭代dict 效率
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6811508.html
Copyright © 2011-2022 走看看