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.
 

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
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
原创粉丝点击