HDU 5009 Paint Pearls 解题报告(DP)

56 篇文章 0 订阅
11 篇文章 0 订阅

Paint Pearls

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


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
 

    解题报告:DP。dp[i]表示涂完前i个珠子需要的代价。显然,dp[i] <= i。根据题目中的规则,可以推导出:
    dp[i] = min( dp[m-1] + k(m, i)^2 )。k(m, i)表示第m个珠子到第i个珠子中不同颜色的个数。因为dp[n] <= n <= 50000,那么当k(m, i)^2 >= 50000时就不用转移了,也就是k(m, i) <= n ^ 0.5。这样复杂度降为O(n * n ^ 1/2),可以接受。
    接下来就是要快速的求得k(m, i)的值。方法也有很多,我这里用链表保存当前位置上j种颜色至多能往前延伸的位置,已经延伸遇到的新颜色。每次位置加1时,进行相应的更新。比较难想到。当然,应该也有其他方法。代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(a);--i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM

    work();
}

void read_int(int&x)
{
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    x = ch - '0';
    ch=getchar();
    while('0'<=ch&&ch<='9')
    {
        x = 10*x + ch-'0';
        ch = getchar();
    }
}

/***************************************************/

int a[55555];
int b[55555];
int dp[55555];
int pre[55555];
int nxt[55555];

void work()
{
    int n;
    while(scanf("%d", &n) == 1)
    {
        fff(i, 1, n)
            scanf("%d", a + i);

        int tot = 0;
        fff(i, 1, n) if(a[i] != a[i-1])
            a[++tot] = a[i];
        n = tot;

        fff(i, 1, n)
        {
            dp[i] = dp[i-1] + 1;

            int p = i;
            pre[p] = i - 1;
            b[i] = a[i];

            int k = 1;
            while(pre[p])
            {
                int t = p;
                p = pre[p];
                if(pre[p] && b[pre[p]] == a[i])
                    pre[t] = pre[p], b[pre[p]] = b[p], p = pre[p];
                k++;
                dp[i] = min(dp[i], dp[p-1] + k*k);

                if(k>=233) break;
            }
        }

        printf("%d\n", dp[n]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值