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 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

因为颜色范围比较大,可以对颜色进行离散化便于处理。然后暴力,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;
}
时间: 2024-10-12 11:50:26

hdu 5009 Paint Pearls (动态规划)的相关文章

HDU 5009 Paint Pearls _(:зゝ∠)_2014 ACM/ICPC Asia Regional Xi&#39;an Online

呵呵 #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> typedef long long ll; using namespace std; const int N = 5 * 10000 + 5; int xval[N], dep; int n, a[N], pre[N]; ll d[N]; int pos[300], dd; void work()

HDU 5009 Paint Pearls(西安网络赛C题)

HDU 5009 Paint Pearls 题目链接 题意:给定一个目标颜色,每次能选一个区间染色,染色的代价为这个区间不同颜色数的平方,问最小代价 思路:先预处理,把相同颜色的一段合并成一个点,然后把颜色离散化掉,然后进行dp,dp[i]表示染到第i个位置的代价,然后往后转移,转移的过程记录下不同个数,这样就可以转移了,注意加个剪枝,就是如果答案大于了dp[n]就不用往后继续转移了 代码: #include <cstdio> #include <cstring> #include

HDU 5009 Paint Pearls (动态规划)

Paint Pearls 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 eac

HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 Accepted 5009 1265MS 1980K 2290 B G++ czy   Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm

HDU 5009 Paint Pearls 双向链表优化DP

Paint Pearls 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 eac

HDU - 5009 Paint Pearls(dp+双向链表优化)

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,

hdu 5009 Paint Pearls (dp)

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,

HDU - 5009 Paint Pearls(dp+优化双向链表)

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,

hdu 5009 Paint Pearls

首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节点之后(不包括k)到第i+1个节点一次涂完,且一起涂的节点共有num种颜色,即dp[i+1] = dp[k] + num * num 从而可以得到状态转移方程dp[i+1] = min(dp[i], dp[k] + num * num) 但是如果从后往前遍历每一个k,会超时. 因此我们可以使用双向链