hdu 5009 Paint Pearls (动态规划)

Paint Pearls

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


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 k 2 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×10 4), indicating the number of pearls. The second line contains a 1,a 2,...,a n (1 ≤ a i ≤ 10 9) 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

因为颜色范围比较大,可以对颜色进行离散化便于处理。然后暴力,G++可以通过。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define ll __int64
#define N 50005
const int inf=0x1f1f1f1f;
int dp[N];
struct node
{
    int val,id,col;
}a[N];
vector<int>g;
int vis[N];
bool cmpval(node a,node b)
{
    return a.val<b.val;
}
bool cmpid(node a,node b)
{
    return a.id<b.id;
}
int main()
{
    int i,j,n,cnt;
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i].val);
        }
        cnt=1;
        for(i=2;i<=n;i++)        //颜色一样的连续石子保留一个
            if(a[i].val!=a[i-1].val)
            a[++cnt]=a[i];
        n=cnt;
        for(i=1;i<=n;i++)  //原始顺序
            a[i].id=i;
        sort(a+1,a+n+1,cmpval);
        a[1].col=0;
        cnt=0;
        for(i=2;i<=n;i++)   //为石子颜色重新编号
        {
            if(a[i].val!=a[i-1].val)
                a[i].col=++cnt;
            else
                a[i].col=cnt;
        }
        sort(a+1,a+n+1,cmpid);
        for(i=0;i<=n;i++)
            dp[i]=inf;
        dp[0]=0;
        dp[n]=n;
        for(i=0;i<n;i++)
        {
            cnt=0;
            for(j=i+1;j<=n;j++)
            {
                if(!vis[a[j].col])
                {
                    cnt++;
                    vis[a[j].col]=1;
                    g.push_back(a[j].col);
                }
                if(dp[i]+cnt*cnt>=dp[n])  //优化
                    break;
                dp[j]=min(dp[j],dp[i]+cnt*cnt);
            }
            for(j=0;j<cnt;j++)   //优化
                vis[g[j]]=0;
            g.clear();
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值