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个数是递增的. 然后使用 ...
随机推荐
- poj 1201 Interval (查分约束)
/* 数组开大保平安. 查分约束: 输入的时候维护st和end 设每个点取元素di个 维护元素个数前缀和s Sbi-Sai-1>=ci 即:建立一条从ai-1到bi的边 权值为ci 表示ai到b ...
- 招行ODC项目表彰
- Webview的使用和注意事项
1.webView是一个展示web网页的控件,继承 AbsoluteLayout 2.webview的俩个回调应用层: 1)webViewClient 这个对象的创建 WebViewClient my ...
- linux command cp.
Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...
- iOS8中添加的extensions总结(一)——今日扩展
通知栏中的今日扩展 分享扩展 Action扩展 图片编辑扩展 文件管理扩展 第三方键盘扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutor ...
- unexpected nil window in _UIApplicationHandleEventFromQueueEvent...
unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: <UI ...
- JavaScript--声明提前
声明提前(hoist): 在正式执行程序前,都会将所有var声明的变量和function声明的函数提前到*当前作用域*的顶部集中创建. 但是,赋值留在原地. console.log(a);//unde ...
- 多个显示器, window.open的定位
// Pops a window relative to the current window position function popup(url, winName, xOffset, yOffs ...
- centos7 服务器安装nginx,mysql,php
一.概述 项目的需要,今天在虚拟机上基于Centos安装配置了服务器运行环境,web服务用 nginx,数据库存储在mysql,动态脚本语言是php. 二.步骤 首页保证Centos7已经安装完毕,正 ...
- thinkphp 文件下载实例 实现以及注意事项
#下载 function download() { $id=$_GET['id']; $file_name ...