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

Problem Description
The Game “Man Down 100 floors” is an famous and interesting game.You can enjoy the game from

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).

 
Input
There are multiple test cases.

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.

 
Output
If the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. But if the man can not fall down onto the floor anyway ,just output “-1”(not including the quote)
 
Sample Input
4
10 5 10 10
5 3 6 -100
4 7 11 20
2 2 1000 10
 
Sample Output
140
 
Source
 
 
代码
 
#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)的更多相关文章

  1. HDU 3016 Man Down(线段树)

    HDU 3016 Man Down 题目链接 题意:是男人就下100层的游戏的简单版,每次仅仅能从两端下落.求落地最大血量 思路:利用线段树能够处理出每一个线段能来自哪几个线段.然后就是dag最长路了 ...

  2. HDU 5125 magic balls(线段树+DP)

    magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  4. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  5. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  6. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  7. hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  8. HDU 4719Oh My Holy FFF 线段树+DP

    /* ** 日期: 2013-9-12 ** 题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个.每个数有一个h[i], ** 每份最右边的那个数要大于前一份最右边的那个数.设每份最右边的 ...

  9. hdu 4747 线段树/DP

    先是线段树 可以知道mex(i,i),mex(i,i+1)到mex(i,n)是递增的. 首先很容易求得mex(1,1),mex(1,2)......mex(1,n) 因为上述n个数是递增的. 然后使用 ...

随机推荐

  1. (转)Vim用法小结

    这是我转的一些vim基本用法,可能对初用者会有帮助,独乐乐不如众乐乐,是吧! Vim一般的Unix和Linux下均有安装.  三种状态 Command: 任何输入都会作为编辑命令,而不会出现在屏幕上 ...

  2. jQuery的live()方法对hover事件的处理示例

    hover([over,]out) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法 当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数. 当鼠标移出这个元素时,会触发指定的第二个 ...

  3. Linq101-CustomSequence

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class CustomS ...

  4. excel - 统计字符个数综合案例

    本文通过一个综合的案例来介绍excel统计字符数的一些方法和思路,供大家参考和学习. 下图是一个excel数据源截图,我们逐一讲解不同条件的统计字符数. 第一,统计A2所有的字符数,不论是汉字和数字. ...

  5. Objective-C中的内存管理——手动内存管理

    一.引用计数(retainCount) 1.内存的申请 alloc和new是Objective-C语言中申请内存的两种常用方式. 当我们向一个类对象发送一个alloc消息的时候,其会向操作系统在堆中申 ...

  6. iOS加载启动图的时候隐藏statusbar + 指定启动图显示多少秒

    只需需要在info.plist中加入Status bar is initially hidden 设置为YES 补充一下,现在手机越来越快,在6+下面启动图一闪而过,而美工童鞋辛辛苦苦做的图就看不到鸟 ...

  7. 基于GBT28181:SIP协议组件开发-----------第二篇SIP组件开发原理

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3937590.html,qq:1269122125. 上一节中讲的S ...

  8. Chrome不支持本地Ajax请求解决?

    今天用JQuery操作Ajax时,使用load方法加载html块 结果提示: XMLHttpRequest cannot load file~~~~~~~Origin 'null' is theref ...

  9. php 手机电话正则表达式验证

            function check_telnum1($telnum)        {                             $b1 = (preg_match(" ...

  10. HTML&CSS基础学习笔记1.28-给网页添加一个css样式

    CSS是什么? 当HTML配合CSS一起使用时,我们发现页面变得好看了很多.那么CSS到底是什么呢? CSS指层叠样式表 (Cascading Style Sheets),它主要是用于定义HTML标签 ...