题目链接:https://cn.vjudge.net/problem/HDU-5009

题意

给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数。

求分割区间的最小代价。n<=5e4

例:1 3 3

答:2

思路

今天刚讲的dp题目,这次讲课收益非常,写个题解。

状态和转移是dp[i]=min(dp[j]+cnt[j-1][i]^2)

会发现这样绝对超时,又发现这里的j可以贪心的跳着选择(因为种数相同时,左端点应越小越好)。

这里的跳着选择想了半天没什么思路,又是最后看了题解-_-

双向链表在排除某一元素时非常有用,以前只用过一次,这次真的是感受到这玩意的通用之处了(话说这个东西应该非常方便,继并查集之后的又一轻量级数据结构)

提交过程

TLE 注意当区间的种类超过sqrt(i)时,不如一个一个选,跳出即可
WA 注意边界dp[0]=0, 而非dp[1]=1
AC

代码

#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5e4+20, INF=0x3f3f3f3f;
int n, clr[maxn], pre[maxn], nex[maxn];
int dp[maxn]; int main(void){
while (scanf("%d", &n)==1 && n){
for (int i=1; i<=n; i++) scanf("%d", &clr[i]); for (int i=1; i<=n; i++) dp[i]=INF;
for (int i=1; i<=n; i++)
pre[i]=i-1, nex[i]=i+1;
pre[0]=-1; nex[n]=-1; dp[0]=0;
map<int, int>id;
for (int i=1; i<=n; i++){
if (!id.count(clr[i])) id[clr[i]]=i;
else{
int idx=id[clr[i]];
pre[nex[idx]]=pre[idx];
nex[pre[idx]]=nex[idx];
id[clr[idx]]=i;
} for (int k=pre[i], cnt=1; k!=-1; k=pre[k], cnt++){
dp[i]=min(dp[i], dp[k]+cnt*cnt);
if (cnt*cnt>i) break;
}
}
printf("%d\n", dp[n]);
} return 0;
}
Time Memory Length Lang Submitted
686ms 3444kB 975 G++ 2018-08-13 08:37:10

HDU-5009 Paint Pearls 动态规划 双向链表的更多相关文章

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

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

  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(dp+优化双向链表)

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

  5. hdu 5009 Paint Pearls

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

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

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

  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. 伍、ajax

    一.ajax的概念 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 传统的网页(不使用 AJAX)如果需要更新 ...

  2. 一个helloword hibernate配置以及查询

    搭建一个Hibernate环境,开发步骤: 1. 下载源码 版本:hibernate-distribution-3.6.0.Final 2. 引入jar文件 hibernate3.jar核心  +   ...

  3. IOS - 总结(网络状态变更)

    - (void)initNetworkMonitor { NSURL *baseURL = [NSURL URLWithString:@"http://www.baidu.com/" ...

  4. Extjs toolbar 如何添加竖杆分隔符

    如下: { xtype:'button', text:'学生档案', iconCls:'file', handler:function(){ console.log(222) }, }, {xtype ...

  5. linux 源码包安装拾遗

    源码包安装和apt-get/yum的区别 安装前的区别:概念上的区别 rpm和dpkg包是经过编译过的包,并且其安装位置由厂商说了算,厂商觉得安装在哪里合适,就会装在哪里,而源码包则是没有经过编译的文 ...

  6. HDU 1002 A + B Problem II( 高精度加法水 )

    链接:传送门 题意:A + B 高精度,板子题 /************************************************************************* & ...

  7. cron 和anacron 、日志转储的周期任务

    一.cron是开机自动启动的 [root@localhost ~]# chkconfig --list | grep "cron" crond 0:off 1:off 2:on 3 ...

  8. 移动端优先的flex三栏布局

    默认情况下先显示移动端,通过 @media 属性适配屏幕变化 使用flexbox相关的CSS属性 display: flex; (父元素) flex-wrap: nowrap | wrap | wra ...

  9. tomcat闪退无法启动 the catalina_home environment variable is not defined correctly this environment variable is needed to run this program

    未成功配置CATALINA_HOME 1.计算机>属性>环境变量, 新建环境变量.变量名为CATALINA_HOME ,变量值tomcat的解压目录,注意后面不用多加“\”或者“;” 2. ...

  10. [luogu] P2569 [SCOI2010]股票交易 (单调队列优化)

    P2569 [SCOI2010]股票交易 题目描述 最近 \(\text{lxhgww}\) 又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,\(\te ...