zoukankan      html  css  js  c++  java
  • hdu5009 Paint Pearls[指针优化dp]

    Paint Pearls

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


    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 

     

    /*
    f[i]表示涂完前適个所化的最小代价
    f[i]=min(f[j]+num(j+1,i)^2);{0<=j<i}
    双向链表优化+剪枝后时间复杂度:O(n√n) 
    */
    #include<map>
    #include<cstdio>
    #include<iostream>
    using namespace std;
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int N=1e5+5;
    int n,a[N];
    int pre[N],nxt[N];
    map<int,int>mp;
    int f[N];
    int main(){
        while(~scanf("%d",&n)){
            mp.clear();fill(f,f+n+1,2e9);f[0]=0;
            for(int i=1;i<=n;i++) a[i]=read();
            for(int i=0;i<=n;i++) pre[i]=i-1,nxt[i]=i+1;
            //双向链表删点好神奇 
            for(int i=1,tmp;i<=n;i++){
                if(!mp.count(a[i])) mp[a[i]]=i;
                else{
                    tmp=mp[a[i]];
                    nxt[pre[tmp]]=nxt[tmp];
                    pre[nxt[tmp]]=pre[tmp];
                    mp[a[i]]=i;                
                }
                for(int j=pre[i],cnt=0;~j;j=pre[j]){
                    cnt++;
                    f[i]=min(f[i],f[j]+cnt*cnt);
                    if(cnt*cnt>n) break;
                }
            }
            printf("%d
    ",f[n]);
        }
        return 0;
    }
  • 相关阅读:
    数据仓库经验小结(转)
    SSIS高级内容 系列一
    SSIS高级内容 系列二
    SSIS数据流组件开发(1)转载
    在sharepoint中添加视频播放
    处理数据仓库中历史数据的若干问题
    SSIS数据流组件开发(2) 转载
    [转载]俄罗斯只剩“大国的脾气”
    [转载]游击队员之歌
    [转载]世界是女人的
  • 原文地址:https://www.cnblogs.com/shenben/p/6710938.html
Copyright © 2011-2022 走看看