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;
}

最新文章

  1. App Widget
  2. gantt甘特图的制作过程
  3. Chrome开发者工具不完全指南(六、插件篇)
  4. Effective java笔记8--序列化
  5. reds pub/sub官方文档翻译
  6. 基于WORDPRESS+MYSQL的绿色企业主题制作全过程
  7. 数据库语言(三):MySQL、PostgreSQL、JDBC
  8. stack 集合栈计算机 (摘)
  9. [转] tomcat组成及工作原理
  10. CodeForces 669D Little Artem and Dance
  11. Ubuntu上搭建Git服务器
  12. Java 基础 程序流程控制 (上)
  13. JavaScript中的比较规则之“==”运算符
  14. Android项目实战(五十三):判断网络连接是否为有线状态(tv项目适配)
  15. jquery.string.js
  16. java_30对文件的操作
  17. 1星|《社群X平台》:没有实际工作经验的职业写手拼凑而成
  18. 1003. [ZJOI2006]物流运输【区间DP+最短路】
  19. Python标准库学习之zipfile模块
  20. Thinkphp 图形验证码无法显示

热门文章

  1. Linq101-Element
  2. 二、T4模板
  3. Django 1.10中文文档—第一个Django应用Part1
  4. js 函数参数形式
  5. macbook Android开发环境搭建,真机调试
  6. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
  7. sql编程 1
  8. Java反射机制(转载)
  9. Sample rate 理解
  10. iOS项目管理:目录结构和开发流程