首页 > 其他分享 >HDU3016-Man Down

HDU3016-Man Down

时间:2022-11-22 21:06:06浏览次数:70  
标签:HDU3016 right int value Down he left dp Man


Man Down

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


Total Submission(s): 2809    Accepted Submission(s): 1024

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 ​HDU3016-Man Down_#include
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 ​

注意多组数据!

虽然说这题大家应该都知道是用线段树,但是为什么呢?

当然,看到这题首先想的不应该是线段树,而是dp。dp还是非常明显,一般人都想得出来。每块板子可以落到哪几块板子上就连一条边,形成一个DAG,在DAG上进行dp就行了。

所以为什么要用线段树呢,在这题中可以发现如果直接dp,时间复杂度会达到n^2,所以用线段树查询每块板子能落到哪几块板子上,将一个n优化成logn。

但是各个板子高低不等啊,所以先把所有板子从低到高进行排序,然后从低到高遍历一遍,先查询左右两边由哪块最高的板覆盖,再将这块板加入线段树中。

Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define N 100005
using namespace std;
int n,dp[N];
typedef long long ll;
struct node
{
int high,left,right,value;
int downleft,downright;
}a[N];
struct tree
{
int left,right,mark,n;
}t[N*4];
int cmp(node x,node y)
{
return x.high<y.high;
}
void build(int o,int l,int r)
{
t[o].left=l;t[o].right=r;
t[o].mark=0;t[o].n=-1;
if(l==r)return;
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
}
int query(int o,int x)
{
if(t[o].left==t[o].right)return t[o].n;
int mid=(t[o].left+t[o].right)>>1;
if(t[o].mark!=-1)
{
t[o<<1].mark=t[o<<1|1].mark=t[o].mark;
t[o<<1].n=t[o<<1|1].n=t[o].n;
return t[o].n;
}
if(x<=mid)return query(o<<1,x);else
return query(o<<1|1,x);
}
void updata(int o,int x,int l,int r)
{
if(t[o].left==l&&t[o].right==r)
{
t[o].mark=1;
t[o].n=x;
return;
}
int mid=(t[o].left+t[o].right)>>1;
if(t[o].mark!=-1)
{
t[o<<1].mark=t[o<<1|1].mark=t[o].mark;
t[o<<1].n=t[o<<1|1].n=t[o].n;
t[o].mark=-1;
}
if(r<=mid)updata(o<<1,x,l,r);else
if(l>mid)updata(o<<1|1,x,l,r);else
{
updata(o<<1,x,l,mid);
updata(o<<1|1,x,mid+1,r);
}
}
int main()
{
while(~scanf("%d",&n))
{
a[0].high=0;a[0].left=1;a[0].right=N;a[0].value=0;
for(int i=1;i<=n;i++)
scanf("%d%d%d%d",&a[i].high,&a[i].left,&a[i].right,&a[i].value);
sort(a+1,a+1+n,cmp);
build(1,1,N);
for(int i=0;i<=n;i++)
{
a[i].downleft=query(1,a[i].left);
a[i].downright=query(1,a[i].right);
updata(1,i,a[i].left,a[i].right);
}
memset(dp,0,sizeof(dp));
dp[n]=100+a[n].value;
for(int i=n;i>=0;i--)
{
int l=a[i].downleft;
int r=a[i].downright;
dp[l]=max(dp[l],dp[i]+a[l].value);
dp[r]=max(dp[r],dp[i]+a[r].value);
}
printf("%d\n",dp[0]=(dp[0]>0?dp[0]:-1));
}
return 0;
}

标签:HDU3016,right,int,value,Down,he,left,dp,Man
From: https://blog.51cto.com/u_15888102/5878472

相关文章

  • CSDN-markdown编辑器用法
    这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你......
  • 【深入浅出 Yarn 架构与实现】4-1 ResourceManager 功能概述
    前面几篇文章对Yarn基本架构、程序基础库、应用设计方法等进行了介绍。之后几篇将开始对Yarn核心组件进行剖析。ResourceManager(RM)是Yarn的核心管理服务,负责集群管......
  • auto.exe/Backdoor.Win32.Agent.bgu,b8u6bvx912.sys/Trojan-Downloader.Win32.Hmir.do
    auto.exe/Backdoor.Win32.Agent.bgu,b8u6bvx912.sys/Trojan-Downloader.Win32.Hmir.don等2endurer原创2008-06-30第1版到​​http://purpleendurer.ys168.com​​下载......
  • 为Markdown/HTML文档生成一个简易目录
    现在阅览文章的网页往往都带有一个目录,方便点击跳转。目录一般都是根据文档中的标题级别直接生成的。现在我们也来模仿一个简单的,无非就是把<h1><h2>...的序列转化成树嘛......
  • 某夏露官方网传播 RootKit.Win32.RESSDT.o/Trojan-Downloader.Win32.Agent.mjp
    某夏露官方网传播RootKit.Win32.RESSDT.o/Trojan-Downloader.Win32.Agent.mjpendurer原创2008-04-09第1版该网站首页包含代码:/---<iframesrc=hxxp://***.look**des*t**.......
  • MakeDown用法
    关于MakeDown的一些数学公式1、上下标^表示上标,_表示下标,如果上标或下标内容多于一个字符,则使用{}括起来。例:\[(x^2+x^y)^{x^y}+x_1^2=y_1-y_2^{x_1-y_1^......
  • Makedown
    from1、上下标^表示上标,_表示下标,如果上标或下标内容多于一个字符,则使用{}括起来。例:(x^2+x^y){xy}+x_1^2=y_1-y_2{x_1-y_12}\[(x^2+x^y)^{x^y}+x_1......
  • 关于Android12安装apk出现-108异常INSTALL_PARSE_FAILED_MANIFEST_MALFORMED的解决方
    原文地址:关于Android12安装apk出现-108异常INSTALL_PARSE_FAILED_MANIFEST_MALFORMED的解决方法-Stars-One的杂货小窝问题描述用户的小米手机上出现以下界面问题小米......
  • postman
    目录请求页签响应页签接口关联内置动态参数以及自定义的动态参数Postman断言数据驱动请求页签Params:get请求传参authorization:鉴权headers:请求头Body:post请求传参......
  • asp.net中ScriptManager是做什么用的
    https://zhidao.baidu.com/question/477322362.html命名空间:System.Web.UI程序集:System.Web.Extensions.dll功能跟Page上的ClientScript差不多,不过主要是用来支持ASP.N......