http://acm.hdu.edu.cn/showproblem.php?pid=5009

2014网络赛 西安 比较难的题

Paint Pearls

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

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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5041 5040 5039 5038 5036

题意:

给出n个数,代表一排珍珠要涂的颜色,刚开始全都没有颜色。可以对连续若干个珍珠发动连涂,代价是这若干珍珠的不同颜色数的平方。涂完这串珍珠,求最小代价和。

题解:

O(n*根号n)的DP,还需要一些超碉方法来处理。

先进行一些小处理,最多5W珍珠,颜色其实也最多5W种,它给的颜色有10^9种,我先用map处理一下变成5w种。

然后连续若干个相同颜色的其实可以合并成一个。经过上面这两个处理,得到了新的珍珠数组。然后才是难点。

先看,最多5W个珍珠,233^2>50000,也就是233连涂就已经超过5W个分别单独涂的代价了。

动态规划,dp[i]表示涂完第i个珍珠所需的代价。

dp[i] = max( dp[i] , dp[k] + j*j)

j为j之后一直到i的不同颜色数。不同颜色数 j 最多为根号n,所以DP是O(n*根号n)的。

i从1到n,j从1到j*j>=i(因为1~i分别单独涂的代价是i)。

关键是如何求每个j对应的k。

假设已知对 i 的每个 j 对应的k,则我们可以求出对 i+1 的每个 j 对应的 k。比较容易想到。具体是这样:

设i的j对应的k为k(i,j)。

b[]为珍珠数组,

若b[i+1]为新出现的颜色,则

  k( i+1 , j+1 ) = k( i , j )

  k( i+1 , 1 ) = i + 1

若b[i+1]为以前就有的颜色,我们用一个last[]数组存每个颜色最后出现的位置,则

对 ( k( i , j ) > last[ b[i+1] ] ) 的j 有 k( i+1 , j+1 ) = k( i , j )

对 ( k( i , j ) < last[ b[i+1] ] ) 的j 则没有变化。

k( i+1 , 1 ) = i + 1

这样我们就能从i 推到 i+1了。

可以发现其中的操作都是连续一堆数向后移动一个下标,如果数组直接搞会TLE。我是用了模拟链表来搞,新颜色出现就直接在后面加,出现旧颜色就把那个分界点删掉,然后在后面加。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("xorin.txt","r",stdin)
#define WE freopen("xorin.txt","w",stdout)
#define mp make_pair
#define pb push_back
const int maxn=;
const int maxm=;
int b[maxn],bn;
int cc[maxm+];
int dp[maxn];
int d[maxn],dn,dq;///d[j]存着某个位置,存了dn个在1~dn,最后一个在dq;
int L[maxn],R[maxn];///L[d[dn]]存着第二个不同的的d[]的下标,再L一下是第三个的
int last[maxn];///last[x]表示x上次出现的位置的d[]的下标
int farm() {
int i,j,k;
// FOR(j,1,bn)printf("%3d",b[j]);
// puts("");
mz(last);
L[]=-;
d[]=;
b[]=;
dn=;
dq=;
d[]=;
L[]=;
R[]=;
last[b[]]=;
FOR(i,,bn) {
dp[i]=i;
j=;
for(k=L[dq]; k!=-; k=L[k]) {
if(cc[j]>=dp[i])break;
//printf("dp[%d]=min(%d , %d + %d)\n",i,dp[i], dp[d[k]],cc[j]);
dp[i]=min(dp[i] , dp[d[k]] + cc[j]);
j++;
}
if(last[b[i+]]==) {
dn++;
R[dq]=dn;
L[dn]=dq;
R[dn]=;
dq=dn;
d[dn]=i+; } else {
j=last[b[i+]];
L[R[j]]=L[j];
R[L[j]]=R[j];
L[j]=dq;
R[j]=;
R[dq]=j;
dq=j;
d[dq]=i+;
}
last[b[i+]]=dq;
}
return dp[bn];
} int a[maxn];
map<int,int>S;
int main() {
int n,i,t;
int x;
FOR(i,,maxm) {
cc[i]=i*i;
} while(RD(n)!=EOF) {
S.clear();
int cnt=;
bn=;
FOR(i,,n) {
RD(x);
int t=S[x];
if(t==) S[x]=t=++cnt;
if(b[bn]!=t) b[++bn]=t;
}
printf("%d\n",farm());
}
return ;
}

有点难,比赛时看了半天不懂,比完了搞了半天也不太懂……搞出来了以后发现思路应该还是挺简单的……

网上找到很多不同的方法,kuangbin菊苣说这个很水,他的并查集的方法我看不太懂……

hdu5009 Paint Pearls (DP+模拟链表)的更多相关文章

  1. hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...

  2. hdu5009 Paint Pearls[指针优化dp]

    Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

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

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  4. HDU-5009 Paint Pearls 动态规划 双向链表

    题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...

  5. HDU 5009 Paint Pearls 双向链表优化DP

    Paint Pearls Problem Description   Lee has a string of n pearls. In the beginning, all the pearls ha ...

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

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

  7. HDU 5009 Paint Pearls (动态规划)

    Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  8. Paint Pearls

    Paint Pearls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009 dp+双向链表优化 看到题目,很自然地可以定义状态:dp[i]表示涂好 ...

  9. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

随机推荐

  1. NuGet打包推送批处理以及MSBuild(通用版)

    使用时注意每个批处理里面的依赖工具路径指向 源码:https://github.com/easonjim/NuGetRun bug提交:https://github.com/easonjim/NuGe ...

  2. PowerDesigner设计表时显示注释选项

    选定编辑的表,[右键]->[Properties]->[Columns]->[Customize Columns and Filter]->[Comment]->[OK] ...

  3. Microsoft SQL Server,错误: 229 解决方案

    今天我在数据库新建一个用户时,碰到了一个奇怪的问题,账号建好了,也指定了该账号上对应的数据库.但是,奇怪的问题出现了,死活不让我新建表,提示如下: 最后,给我找到问题所在了,就是在指定表权限的时候权限 ...

  4. git命令的使用 【备用】

    *下面为对本地仓库进行操作 [将文件提交到本地仓库] git add 文件名                     将文件添加到暂存区 git status                      ...

  5. maven仓库没有的包依赖

    如果有个jar包是我们自己打的,怎么放到maven中呢? 首先在项目里面新建一个lib目录,如果有lib目录则不需要新建,然后放自己的jar包进去,maven的pom.xml配置是: <depe ...

  6. Character literal must contain exactly one character -- 一天一点小知识

    编程语言高度抽象化以后,错误也越来越让人难以理解了, NET编程最常见的一个错误, Object not set to the reference ,过了好久,才明白过来, 就是不明白为啥微软不说   ...

  7. Mysql学习笔记(四)聊聊数据库索引

    小心情(可直接跳到分割线后) 今天心情好些了.一些浓的化不开的坏情绪,也渐渐的在晚上解决掉一个复杂的逻辑问题后,渐渐消散了. 今天中午去吃饭的时候,坤哥漫不经心的说:'我这么多年终于悟出了一个道理,人 ...

  8. 工作流、业务流程管理和SOA

    http://www.cnblogs.com/shanyou/archive/2009/03/29/1424213.html 工作流定义: The  automation of a business ...

  9. 使用Java中的动态代理实现数据库连接池

    2002 年 12 月 05 日 作者通过使用JAVA中的动态代理实现数据库连接池,使使用者可以以普通的jdbc连接的使用习惯来使用连接池. 数据库连接池在编写应用服务是经常需要用到的模块,太过频繁的 ...

  10. SQLServer------解决IP地址登录不了数据库问题

    1.找到配置管理器,打开 2.关掉SQLExpress,打开MSSQLServer 3.配置MSSQLServer(启用Named Pipes和TCP/IP) 4.修改TCP/IP属性(端口:1433 ...