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
2009 Multi-University Training Contest 12 - Host by FZU
代码
#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——线段树

    传送门 Problem Description The Game "Man Down 100 floors" is an famous and interesting game.Y ...

  2. P1295 [TJOI2011]书架(线段树dp)

    P1295 [TJOI2011]书架(线段树dp) 我好菜 先考虑普通dp: d p i = m i n ( d p j + m a x ( h j + 1 , h j + 2 - , h i ) ) ...

  3. hdu 3397 Sequence operation(线段树,lazy,区间合并)

    hdu 3397 Sequence operation 线段树lazy和区间合并结合的一个题,相当于几个题集中到一起嘛,分开想就好了 0,1,2操作都要lazy,2的异或操作找到每一只含1或只含0的区 ...

  4. HDU 6447 YJJ's Salesman(线段树+DP)

    YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  5. HDU 3698-Let the light guide us(线段树+DP)愿圣光忽悠你

    题意: 给定两个矩阵,第一个矩阵代表时间,第二个矩阵代表距离.给出约束的公式,询问在每一行都造一个圣光塔最少花费多少时间! 思路: 对于题目中给的条件 |J-K| <=f[i][j] +f[i+ ...

  6. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  7. CodeForces - 1557D Ezzat and Grid(线段树+dp)

    题目链接:点击查看 题目大意:给出 nnn 个 010101 串,现在问最少需要删掉多少个串,才能使得剩下的串拼起来是连通的 规定两个 010101 串是连通的,当且仅当存在至少一列,在两个串中都为 ...

  8. CodeForces - 487B Strip(线段树+dp+二分)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,现在要求分成尽可能少的子段,且每个子段需要满足: 最大值与最小值的差值小于等于 s 子段长度大于等于 l 题目分析:dp[ i ] 代表的是前 ...

  9. hdu 2871 Memory Control(线段树)

    题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放全部空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内 ...

最新文章

  1. 【翻译】使用新的Sencha Cmd 4命令app watch
  2. postgresql 获取所有表名、字段名、字段类型、注释
  3. 打包无法识别lombok
  4. 大数据互联网架构阶段 数据库三范式与反范式
  5. HDU 1421 搬寝室 解题报告(超详细)
  6. 学号20145209《信息安全系统设计基础》第11周学习总结
  7. Jquery对象和DOM对象---Jquery API (1)
  8. golang key map 所有_Map的底层实现 为什么遍历Map总是乱序的
  9. 【今日CV 视觉论文速览】05 Dec 2018
  10. C语言中基础数据类型的取值范围——整型溢出问题
  11. unity3D学习笔记2
  12. 倾斜摄影三维模型五种常见格式
  13. 计算机的kb和m之间的换算,g和兆的换算(G和M之间的换算)
  14. mysql数据库查询总条数
  15. android win8 磁贴效果第三方库,Win8巧用动态磁贴让浏览更轻松
  16. [编译链接装载系列]之聊聊目标文件与ELF格式
  17. 五、GNSS测量控制网的建立(1)
  18. python x 0b1010_下面代码的输出结果是
  19. 青龙面板搭建 纯小白教程
  20. 位运算的那些事(三)位掩码

热门文章

  1. idea创建动态web项目打开jsp文件报404问题
  2. Illustrator 教程:如何在 Illustrator 中更改图稿颜色?
  3. 爱情不是别人给与,励志爱情歌曲《一张纸》让你大气去爱
  4. 常见文件类型扩展名及其类型说明
  5. Java自学练习代码页
  6. 8.Java实现数字的千分制
  7. ENDIAN的由来及BIG-EDIAN 和LITTLE-ENDIAN
  8. python画魄罗代码_LOL2月5日红贴 慎嘲讽技能Bug将修复魄罗英雄新原画
  9. mysql语句 execute、executeQuery和executeUpdate之间的区别
  10. 材质常用节点以及用法