HDU - 5009-Paint Pearls

Paint Pearls

我的github代码地址:点这

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
 
 
题目意思:给定一系列目标颜色,每次能选一个区间染色,染色的代价为这个区间不同颜色数的平方,问最小代价
 
 
这是一个dp题目,dp转移方程为dp[i]=min{dp[i],dp[j]+num(j+1,i)^2}
 
num(j+1,i)表示序号j+1到i中珍珠颜色的个数。
开始我想用一个二维数据来存储num(j+1,i),但是由于范围是5×104,所以会爆内存和时间而且dp需要两重循环,所以就没办法做了。
 
大神思路:

双向链表优化dp。dp[i]表示涂完前i个所花的最小代价,显然有dp[i]=min{dp[i],dp[j]+num(j+1,i)^2},其中1<=j<i,num(j+1,i)表示区间[j+1,i]的颜色个数。

这样复杂度为O(n^2)显然超时。那么需要优化一下,比如第二组测试数据3 4 2 4 4 2 4 3 2 2,假设dp[1]…dp[8]已更新完毕,现在要更新dp[9],可以看到a[9]为2,按照原始的dp[i]=min{dp[i],dp[j]+num(j+1,i)^2},

i=9,枚举j=8,dp[9]=min{dp[9],dp[8]+1^2};j=7,dp[9]=min{dp[9],dp[7]+2^2};现在貌似没什么变化。。。

j=6,这里就神奇了,如果dp[9]=min{dp[9],dp[6]+3^2},那么这个就太弱了,因为此时2 4 3是连着涂的,但是2之前是3 4 2 4 4,这些如果跟着一起涂,那么仍然是3^2的代价,但前面的数字变少了,显然这种更优。

于是乎dp[9]=min{dp[9],dp[0]+3^2},可以看到6直接跳到了0,为什么这么跳?因为这之前都是些234啊,重复了没必要保存。所以在dp时,我们只需要维护好前后关系即可。

比如当前dp第i位,那么即a[i]加进来,所以之前如果有a[i]值的必须删掉,具体双向链表维护。因此可以看到任意时刻,每种颜色只会保存一次,复杂度就降下来了。

但仍然可以给出坑爹的数据,比如1 2 3 4 … n那么这个dp的话,复杂度仍为O(n^2),于是继续优化。我们知道如果一个一个涂,那么需要花费n。所以最优方案不能大于n,也就是不能连着sqrt(n)个不同的颜色一起涂,否则代价大于n了,这里进行剪枝。复杂度降为O(nsqrt(n)),还是可以接受的。

 

/*
    data:2018.04.15
    author:gsw
    link:https://vjudge.net/contest/222656#discuss
    account:tonygsw
*/
#include<set>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define IO ios_base::sync_with_stdio(0);
const int inf=0x3f3f3f3f;

const int maxn=50005;
int n,len;
int pearl[maxn];
int dp[maxn];
int pre[maxn],nex[maxn];
void init()
{
    len=1;
    memset(pearl,0,sizeof(pearl));
    for(int i=0;i<maxn;i++)
        dp[i]=inf;
    memset(pre,0,sizeof(pre));
    memset(nex,0,sizeof(nex));
}
int DP()
{
    map<int,int>m;
    pre[0]=-1;dp[0]=0;
    for(int i=1;i<=n;i++)
    {
        if(!m.count(pearl[i]))
            m[pearl[i]]=i;
        else
        {
            int tem=m[pearl[i]];
            pre[nex[tem]]=pre[tem];//把之前的删除掉,维护双向链表 
            nex[pre[tem]]=nex[tem];
            m[pearl[i]]=i;
        }
        int cnt=0;
        for(int j=pre[i];j!=-1;j=pre[j])
        {
            cnt++;
            if(cnt*cnt>i)break;
            dp[i]=min(dp[i],dp[j]+cnt*cnt);
        }
    }
    return dp[n];
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();int tem;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&pearl[i]);
            pre[i]=i-1;
            nex[i]=i+1;
        }
         
        cout<<DP()<<endl;
    }
}

 

 
 
posted @ 2018-04-23 16:02  fantastic123  阅读(97)  评论(0编辑  收藏  举报