hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化
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实现去重优化的更多相关文章
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
- HDU 5009 Paint Pearls (动态规划)
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- hdu 5009 Paint Pearls
首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...
- HDU-5009 Paint Pearls 动态规划 双向链表
题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...
- HDOJ 5009 Paint Pearls
Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
随机推荐
- Java ee第六周作业
JSF 生命周期: FacesServlet 充当用户和 JSF 应用程序之间的纽带.它在明确限定的 JSF 生命周期(规定了用户请求之间的整个事件流)的范围内工作. 1. 当JSF页面上的一个事 ...
- SPOJ Distinct Substrings SA
正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...
- 使用spring data solr 实现搜索关键字高亮显示
后端实现: @Service public class ItemSearchServiceImpl implements ItemSearchService { @Autowired private ...
- java框架之SpringBoot(6)-Restful风格的CRUD示例
准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...
- 41A
#include <stdio.h> #include <string.h> #define MAXSIZE 105 int main() { char Berlandish[ ...
- QCon2019全球软件开发大会广州站即将来袭
QCon广州2019|全球软件开发大会 会议时间:2019-05-25 08:00至 2019-05-28 18:00结束 会议地点: 广州 广州万富希尔顿酒店 中国广州市白云区云城东路515-5 ...
- Docker:Windows7下使用docker toolbox(1)
一.安装 官方网址:https://docs.docker.com/docker-for-windows/install/ win10以下安装:https://www.docker.com/produ ...
- Cycle (KMP + hash)
题意:给你2个串,让你判断2个字符串的前缀是否满足首尾连接形成的环是不是一样的. 思路:我们需要提前知道的是满足条件的前缀一定满足 strA = str1 + str2, strB = str2 + ...
- 【Spark-core学习之三】 Spark集群搭建 & spark-shell & Master HA
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...
- SpringMvc HandlerMappings 何时初始化?
SpringMvc 的转发控制器 DispatcherServlet 执行 initStrategies(),在什么时候初始化 HandlerMappings ? 在容器 AbstractApplic ...