hdu3016 线段树+简单DP
以每个方块左右坐标区间为节点建立字典树,每个节点保存这个区间对应的方块的下标,将方块按照高度排序。
如何得到第i个方块可以移动到的两个方块呢?将所有方块排完序,将前i-1个方块放入字典树,根据第i个方块的左右坐标,分别进行单点查询即可,将下一个位置保存。
最后的DP转移方程d[i]=max(d[i].left,d[i].right)+d[i].val;
AC代码:
#include<cstdio> #include<algorithm> using namespace std; const int maxn=1e5+5; struct node{ int l,r,ind; }tree[maxn*4]; struct point{ int l,r,h,val; point(){ } point(int h,int l,int r,int val):h(h),l(l),r(r),val(val){ } bool operator <(const point &p)const{ return h<p.h; } }plank[maxn]; void Build(int l,int r,int cur){ //建树并初始化为floor tree[cur].l=l; tree[cur].r=r; tree[cur].ind=0; if(l==r) return; Build(l,(l+r)>>1,cur<<1); Build((l+r)/2+1,r,(cur<<1)+1); } int ind; void Insert(int l,int r,int cur){ //更新区间的值 //printf("%d %d\n",l,r); int ll=tree[cur].l,rr=tree[cur].r; if(l==ll&&r==rr) { tree[cur].ind=ind; return; } int mid=(ll+rr)>>1; if(r<=mid) Insert(l,r,cur<<1); else if(l>=mid+1) Insert(l,r,(cur<<1)+1); else { Insert(l,mid,cur<<1); Insert(mid+1,r,(cur<<1)+1); } } int val; int Search(int cur){ //搜索可以到达的下一个格子区间,返回下标 int l=tree[cur].l,r=tree[cur].r; if(l==r) return tree[cur].ind; int ind=tree[cur].ind; int mid=(l+r)>>1; int a; if(val<=mid) a=Search(cur<<1); else a=Search((cur<<1)+1); return plank[ind].h>plank[a].h?ind:a; } int main(){ plank[0]=point(0,0,0,0); int n; while(scanf("%d",&n)==1){ int l,r,h,v,width=-1; for(int i=1;i<=n;++i) { scanf("%d%d%d%d",&h,&l,&r,&v); width=max(width,r); plank[i]=point(h,l,r,v); } Build(1,width,1); sort(plank+1,plank+n+1); //按高度排序 for(int i=1;i<=n;++i){ int l=plank[i].l,r=plank[i].r; val=l; plank[i].l=Search(1); val=r; plank[i].r=Search(1); //printf("%d %d\n",plank[i].l,plank[i].r); ind=i; Insert(l,r,1); } for(int i=1;i<=n;++i){ int l=plank[i].l,r=plank[i].r; plank[i].val+=max(plank[l].val,plank[r].val); } if(plank[n].val+100<=0) printf("-1\n"); else printf("%d\n",plank[n].val+100); } return 0; }
如有不当之处欢迎指出!
hdu3016 线段树+简单DP的更多相关文章
- 2018.09.12 poj2376Cleaning Shifts(线段树+简单dp)
传送门 貌似贪心能过啊%%%. 本蒟蒻写的线段树优化dp. 式子很好推啊. f[i]表示覆盖1~i所需的最小代价. 那么显然对于一个区间[li,ri]" role="present ...
- 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- 【题解】P4247 [清华集训]序列操作(线段树修改DP)
[题解]P4247 [清华集训]序列操作(线段树修改DP) 一道神仙数据结构(DP)题. 题目大意 给定你一个序列,会区间加和区间变相反数,要你支持查询一段区间内任意选择\(c\)个数乘起来的和.对1 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
随机推荐
- Java的IO系统
Java IO系统 "对语言设计人员来说,创建好的输入/输出系统是一项特别困难的任务." 由于存在大量不同的设计方案,所以该任务的困难性是很容易证明的.其中最大的 ...
- libev学习笔记
转 libev的使用--结合Socket编程 作者:cxy450019566 之前自己学过一些libev编程的基础,这次写压测刚好用上了,才算真正动手写了些东西,在这里做一些总结.写这篇文章是为了用浅 ...
- 将centos_yum源更换为阿里云(官方文档)
http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0.d6ykiD 1.备份 mv /etc/yum.repos.d/CentOS ...
- nginx把POST转GET请求解决405问题
405重定向,然后把POST转GET upstream local { server 10.0.1.11:81; } server { listen 81; server_name testf.xxx ...
- 01-Go命令与基础
什么是Go? Go是一门并发支持.垃圾回收的编译型系统编程语言,旨在创造一门具有在静态编译语言的高性能和动态的高效开之间拥有良好平衡点的一门编程语言. Go的主要特点有哪些? 类型安全和内存安全 以非 ...
- 百度网盘不限速下载软件 Pan Download
百度网盘不限速下载软件 Pan Download Pan Download下载软件是一款电脑端的快速下载器软件,您可以通过Pan Download直接下载百度网盘中的资源,此款软件下载速度快,下载压缩 ...
- 【Thinkphp 5】 整合邮箱类 phpmailer实现邮件发送
第一步:下载phpmailer文件,主要用到的文件只有箭头指向的两个,thinkphp5中,把class.phpmailer.php改成了phpmailer.php 第二步: 将phpmailer文件 ...
- Python Selenium + phantomJS 模拟登陆教务管理系统 “抢课”
# _*_coding:utf-8_*_ from selenium import webdriver from selenium.webdriver.common.action_chains imp ...
- [Android] Toast问题深度剖析(一)
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 题记 Toast 作为 Android 系统中最常用的类之一,由于其方便的api设计和简洁的交互体验,被我们所广泛采用.但是,伴随着我们开发的深 ...
- 老凡原创PHPcms发布上架chinaz
FanCms内容管理系统(3网兼容) FanCms内容管理系统(3网兼容)核心使用CodeIgniter框架.使用SQlite数据库,不需要安装,上传到任意支持PHP的空间,就能使用.高级URL自定义 ...