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

题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方。

这题我们显然会首先想到用DP去解决,dp[i] = min( dp[i] , dp[j] + cost(i , j) )。但是枚举ij的复杂的高达n^2,不能接受,我们就考虑去优化它。

首先比较容易想到的是,去除连续重复元素,可以把它们当作同一个点处理。

此外在遍历j的过程中如果当前序列颜色不同的数量平方大于当前dp[i],显然已经没有一并涂色以及继续扩充序列的必要了(随着序列数的增长,不同颜色的数量是单调递增的,必然在之后不会出现小于dp[i]的情况。

但是这样并不能把复杂的降低至sqrt(n)*n,因为我们枚举的子序列会有重复元素,所以并不是每次j的变化都会来带不同颜色数的增长,比如24242424这种。那么我们考虑这种情况该如何优化,我们先换一个比较容易理解的例子9871341,当我们的i指向最后一个1的时候,我们的j开始向前遍历,当遍历到上一个i的时候,其实我们已经知道,这个i我们把它包含进去是没有成本的(因为之前已经有1了,并不会使不同颜色的数增加)所以我们应该不假思索地加入这个数。但是计算机是很蠢的,它每次依旧会遍历到它,别看这遍历一个单位很快,像24242424这种就会导致计算机大量重复地枚举了。为此我们模拟一个双向链表,在枚举i地过程中,我们维护前缀序列地链表全都是不同的元素,这个实现起来其实并不难,因为i每次增长最多会增加一个重复元素,相应的我们也只需要去除一个重复元素(即上一次出现当前值的那个位置)。然后在遍历j的时候只需要遍历链表就好了。这样我们保证j遍历过程中每次都能增加一个不同颜色,复杂的自然降低到n*sqrt(n)了。

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cstdio>
#define LL int
using namespace std;
const int N=;
const LL inf=1e8+;
LL arr[N];
LL dp[N];
int pre[N];
int nex[N];
int main()
{
cin.sync_with_stdio(false);
int n;
while(cin>>n)
{
int p=;
for(int i=; i<n; i++)
{
int c;
//c=_read();
cin>>c;
if(i==||arr[p]==c)
arr[p]=c;
else
arr[++p]=c;
}
map<LL,int> unq;
int rk=;
for(int i=; i<=p; i++)
{
map<LL,int>::iterator it=unq.find(arr[i]);
if(it!=unq.end())
arr[i]=it->second;
else
{
unq[arr[i]]=rk;
arr[i]=rk;
rk++;
}
pre[i]=i-;
nex[i]=i+;
}
dp[p+]=;
map<LL,int> v;
for(int i=p; i>=; i--)
{
LL ans=inf;
if(v.find(arr[i])==v.end())
v[arr[i]]=i;
else
{
int ix=v[arr[i]];
nex[pre[ix]]=nex[ix];
pre[nex[ix]]=pre[ix];
v[arr[i]]=i;
}
int rx;
int cnt=;
for(int j=i; j!=p+; j=nex[j])
{
//cout<<j<<endl;
cnt++;
LL p2=cnt*cnt;
if(p2>ans)
break;
if(p2+dp[nex[j]]<ans)
{
ans=p2+dp[nex[j]];
rx=j;
}
//ans=min(ans,(LL)(xx+dp[j+1]));
//cout<<ans<<' '<<s.size()<<' '<<dp[j+1]<<endl;
}
dp[i]=ans;
//cout<<dp[i]<<' '<<i<<' '<<rx<<endl;
}
//printf("%d\n",dp[0]);
cout<<dp[]<<endl;
}
return ;
}

hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu 5009 Paint Pearls

    首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...

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

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

  7. HDOJ 5009 Paint Pearls

    Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...

  8. AC日记——Paint Pearls hdu 5009

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

  9. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

随机推荐

  1. ExceptionLess使用

    1.os使用windows server 2012,省的升级iis express-iis 8(测试环境是IIS Express 8,生产环境IIS 7.5).PowerShell 3+了. 2.wi ...

  2. (Detected problems with API compatibility(visit g.co/dev/appcompat for more info)

    在applicaiton里面加载这么一段代码: private void closeAndroidPDialog(){ try { Class aClass = Class.forName(" ...

  3. [sql]sql的select字符串切割

    可以经常看看 mysql的refman,写的很棒 sql基础操作 查看表结构 show create table desc table show full columns from test1; li ...

  4. 133A

    #include <stdio.h> #include<string.h> #include <stdbool.h> #define MAXSIZE 105 int ...

  5. osx brew mysql

    MariaDB Server is available for installation on macOS (formerly Mac OS X) via the Homebrew package m ...

  6. Kafka笔记1(初步认识)

    Kafka 被称为“分布式提交日志”或“分布式流平台” 文件系统或数据库提交日志用来提供所有事务的持久记录,通过重放这些日志重建系统状态,同时Kafka数据是按照一定顺序持久化保存的,可以按需读取 K ...

  7. 删除本地git的远程分支和远程删除git服务器的分支

    在项目中使用git管理代码后,有些时候会创建很多不同名称的分支,以此区分各个分支代码功能. 而随着代码的合并,以前的分支就可能不再需要保存了,所以就要对没有用的分支进行删除,包括紧急回滚时从中抽取某一 ...

  8. Redis初探-Redis安装

    官网地址:https://redis.io/download 最新版本是4.0,在这里本人下的是3.2 使用rz命令可以将Redis上传到Linux系统 首先要确定Linux上是否安装了gcc,没有则 ...

  9. 51Nod 1058 N的阶乘的长度

    输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.   Input 输入N(1 <= N <= 10^6) Output 输出N的阶乘的长度 Input示例 6 Out ...

  10. P2365 任务安排 / [FJOI2019]batch(斜率优化dp)

    P2365 任务安排 batch:$n<=10000$ 斜率优化入门题 $n^{3}$的dp轻松写出 但是枚举这个分成多少段很不方便 我们利用费用提前的思想,提前把这个烦人的$S$在后面的贡献先 ...