HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1391 Accepted Submission(s): 483
http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html
We take a simplified version of this game. We have only two kinds of planks. One kind of the planks contains food and the other one contains nails. And if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. The man can only fall down vertically .We assume that the energy he can increase is unlimited and no borders exist on the left and the right.
First the man has total energy 100 and stands on the topmost plank of all. Then he can choose to go left or right to fall down. If he falls down from the position (Xi,Yi),he will fall onto the nearest plank which satisfies (xl <= xi <= xr)(xl is the leftmost position of the plank and xr is the rightmost).If no planks satisfies that, the man will fall onto the floor and he finishes his mission. But if the man’s energy is below or equal to 0 , he will die and the game is Over.
Now give you the height and position of all planks. And ask you whether the man can falls onto the floor successfully. If he can, try to calculate the maximum energy he can own when he is on the floor.(Assuming that the floor is infinite and its height is 0,and all the planks are located at different height).
For each test case, The first line contains one integer N (2 <= N <= 100,000) representing the number of planks.
Then following N lines representing N planks, each line contain 4 integers (h,xl,xr,value)(h > 0, 0 < xl < xr < 100,000, -1000 <= value <= 1000), h represents the plank’s height, xl is the leftmost position of the plank and xr is the rightmost position. Value represents the energy the man will increase by( if value > 0) or decrease by( if value < 0) when he falls onto this plank.
10 5 10 10
5 3 6 -100
4 7 11 20
2 2 1000 10
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=100000; struct node{
int l,r,id;
int lazy;
}a[4*maxn]; struct line{
int l,r,lid,rid,h,c,id;
bool friend operator < (line x,line y){
return x.h<y.h;
}
}data[maxn+100]; int n,dp[maxn]; void input(){
data[0].l=0,data[0].r=100000,data[0].h=0,data[0].c=0;
for(int i=1;i<=n;i++){
scanf("%d%d%d%d",&data[i].h,&data[i].l,&data[i].r,&data[i].c);
}
sort(data,data+n+1);
for(int i=0;i<=n;i++){
dp[i]=-1;data[i].id=i;
data[i].lid=-1;data[i].rid=-1;
}
} void build(int l,int r,int k){
a[k].l=l;a[k].r=r;a[k].id=0;a[k].lazy=-1;
if(l<r){
int mid=(l+r)/2;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
}
} void pushDown(int k){
if(a[k].lazy<0) return;
a[2*k].lazy=a[k].lazy;
a[2*k].id=a[k].lazy;
a[2*k+1].lazy=a[k].lazy;
a[2*k+1].id=a[k].lazy;
a[k].lazy=-1;
} int query(int l,int r,int k){
if(l<=a[k].l && a[k].r<=r){
return a[k].id;
}else{
pushDown(k);
int mid=(a[k].l+a[k].r)/2;
if(r<=mid) return query(l,r,2*k);
else return query(l,r,2*k+1);
}
} void insert(int l,int r,int k,int id){
if(l<=a[k].l && a[k].r<=r){
a[k].id=id;
a[k].lazy=id;
}else{
pushDown(k);
int mid=(a[k].l+a[k].r)/2;
if(r<=mid) insert(l,r,2*k,id);
else if(l>=mid+1) insert(l,r,2*k+1,id);
else{
insert(l,mid,2*k,id);
insert(mid+1,r,2*k+1,id);
}
}
} void computing(){
build(0,maxn,1);
for(int i=1;i<=n;i++){
data[i].lid=query(data[i].l,data[i].l,1);
data[i].rid=query(data[i].r,data[i].r,1);
insert(data[i].l,data[i].r,1,data[i].id);
}
dp[n]=100+data[n].c;
for(int i=n;i>=1;i--){
//cout<<data[i].h<<" "<<data[i].lid<<" "<<data[i].rid<<endl;
if(data[i].lid>=0){
int id=data[i].lid;
dp[id]=max(dp[id],dp[i]+data[id].c);
}
if(data[i].rid>=0){
int id=data[i].rid;
dp[id]=max(dp[id],dp[i]+data[id].c);
}
}
if(dp[0]>0) cout<<dp[0]<<endl;
else cout<<"-1"<<endl;
} int main(){
while(scanf("%d",&n)!=EOF){
input();
computing();
}
return 0;
}
HDU 3016 Man Down (线段树+dp)的更多相关文章
- HDU 3016 Man Down(线段树)
HDU 3016 Man Down 题目链接 题意:是男人就下100层的游戏的简单版,每次仅仅能从两端下落.求落地最大血量 思路:利用线段树能够处理出每一个线段能来自哪几个线段.然后就是dag最长路了 ...
- HDU 5125 magic balls(线段树+DP)
magic balls Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- hdu 4117 GRE Words (ac自动机 线段树 dp)
参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...
- hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- HDU 4719Oh My Holy FFF 线段树+DP
/* ** 日期: 2013-9-12 ** 题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个.每个数有一个h[i], ** 每份最右边的那个数要大于前一份最右边的那个数.设每份最右边的 ...
- hdu 4747 线段树/DP
先是线段树 可以知道mex(i,i),mex(i,i+1)到mex(i,n)是递增的. 首先很容易求得mex(1,1),mex(1,2)......mex(1,n) 因为上述n个数是递增的. 然后使用 ...
随机推荐
- ASP.NET-FineUI开发实践-13(一)
开原版好像是没有gird树,有一个扩展列下的模拟树列,就是不能展开,专业版有,开原版我弄了弄,思路是有,就是不是很好实现.这篇博客直接写出了(一)说明一个肯定是写不完的. FineUI重在封装,改这个 ...
- 刷新的时候jquery获取checkbox是否为选中和设置选中
$(document).ready(function(){ $('.uninstall_list_checkbox').click(function(){ if($(this).parent('.un ...
- Datables wrning(table id='example'):Cannot reinitialise DataTable.
出现的问题如下所示: Datables wrning(table id='example' Datables object for this table,please pass eithser no ...
- iOSUI基础——懒加载
1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...
- IOS常用CGRect的交错,边缘,中心的检测
转自:http://tsyouaschen.iteye.com/blog/1946957 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数 BOOL conta ...
- 什么是NSAssert?
断言, 判断是否符合某个特定条件, 符合就继续运行程序, 反之就抛出异常, 后面为自定义错误提示, 也可以使用NSParameterAssert, 在调试上有着很大的方便 int a = 0; NSA ...
- 二、C# 数据类型
C#语言的基本类型包括8种整数类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十 进制浮点类型.1种布尔类型以及1种字符类型. 2.1 基本数值类型 C#中的基本数据类型都有关键字和它们关联 ...
- SpringMVC介绍之约定优于配置
SpringMVC介绍之约定优于配置 所谓的约定优于配置就是指在程序开发过程中我们约定好一些规则可以使我们更少的进行配置和代码编写.就这么简单的一句话可能你还不是很懂什么是约定优于配置,没关系,看完后 ...
- 广东移动NGBOSS系统话费查询
基于很多客户的需要 现承接广东移动NGBOSS华为系统的各项功能开发 承接广东深圳.佛山.东莞.广州.惠州.汕头.湛江移动NGBOSS的全球通开户,批量话费查询.缴费,号码导出等功能开发. 有需要者联 ...
- [jQuery编程挑战]001:实现页面元素加速动画效果
要求: 页面包含两个HTML元素:一个按钮,一个小方块 动画要求:点击按钮,小方块从页面坐标300,300,加速移动到0,0 相关知识点: jQuery动画方法animate easing参数的设置 ...