博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3016 dp+线段树
阅读量:6897 次
发布时间:2019-06-27

本文共 4243 字,大约阅读时间需要 14 分钟。

 

Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2030    Accepted Submission(s): 743

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
 
/*hdu 3016 dp+线段树感觉一直没什么思路由于每次我们只能从两边跳下来,倒推的答案是唯一的于是先按照高度排个序,然后就类似于叠木板,先判断当前的两端下面是哪个木板然后取较大值再加上当前值即可study~~~hhh-2016-02-29 22:54:23*/#include 
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int maxn = 100050;const int inf = 0x3f3f3f3f;struct node{ int l,r,mid; int cov;} tree[maxn<<2];struct segment{ int l,r,h; int val; void get() { scanf("%d%d%d%d",&h,&l,&r,&val); }} seg[maxn];int dp[maxn];bool cmp(segment a,segment b){ return a.h < b.h;}void push_up(int r){}void build(int i,int l,int r){ tree[i].l = l; tree[i].r = r; tree[i].cov = 0; tree[i].mid = (l +r)>>1; if(l == r) { return ; } build(i<<1,l,tree[i].mid); build(i<<1|1,tree[i].mid+1,r); push_up(i);}void push_down(int r){ int lson = r<<1,rson = r<<1|1; if(tree[r].cov != -1) { tree[lson].cov = tree[r].cov; tree[rson].cov = tree[r].cov; tree[r].cov = -1; }}void update(int i,int l,int r,int c){ if(tree[i].l >= l && tree[i].r <= r) { tree[i].cov = c; return ; } push_down(i); if(l <= tree[i].mid) update(i<<1,l,r,c); if(r > tree[i].mid) update(i<<1|1,l,r,c); push_up(i);}int query(int i,int k){ if(tree[i].cov != -1) { return tree[i].cov; } push_down(i); if(k <= tree[i].mid) return query(i<<1,k); else return query(i<<1|1,k); push_up(i);}int main(){ int n; while(scanf("%d",&n) != EOF) { build(1,1,maxn); for(int i =1; i <= n; i++) { seg[i].get(); } memset(dp,0,sizeof(dp)); sort(seg+1,seg+n+1,cmp); int l=0,r=0; for(int i = 1; i <= n; i++) { if(i > 1) { l = query(1,seg[i].l); r = query(1,seg[i].r); } dp[i] = max(dp[l],dp[r])+seg[i].val; update(1,seg[i].l,seg[i].r,i); } dp[n] += 100; if(dp[n] <= 0) printf("-1\n"); else printf("%d\n",dp[n]); } return 0;}

  

 

转载于:https://www.cnblogs.com/Przz/p/5409613.html

你可能感兴趣的文章
细说ASP.NET Forms身份认证
查看>>
需求变化与IoC
查看>>
The copy of Windows is not genuine-微软自己用盗版
查看>>
浮点数精度
查看>>
[置顶] IT屌丝的离职申请
查看>>
IE7 float:left失效的解决方法
查看>>
WordPress 插件机制的简单用法和原理(Hook 钩子)
查看>>
dbchart
查看>>
GCC指令
查看>>
[转] Exchange Server 2013部署
查看>>
I-129表
查看>>
ImageView setImageURI图片不改变\NetWorkImageView 不显示的问题
查看>>
Flash xml 中文乱码
查看>>
BZOJ 2727 双十字(树状数组)
查看>>
8.2 C++ AMP advanced concepts
查看>>
Linux Mint 11正式版发布!
查看>>
C++开发者快速学习Objective-C语言核“.NET研究”心语法
查看>>
(总结)Nginx使用的php-fpm的两种进程管理方式及优化
查看>>
我的KT库之----数据库的操作(DbHelper)
查看>>
js Grid - 列表插件
查看>>