Gym - 101911B Glider【尺取】

参考大佬的博客https://blog.csdn.net/mmk27_word/article/details/85063325?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160153852919724835851673%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160153852919724835851673&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v3~pc_rank_v2-2-85063325.first_rank_ecpm_v3_pc_rank_v2&utm_term=Gym±+101911B&spm=1018.2118.3001.4187

题目

传送门
在这里插入图片描述

Input
3 4
2 5
7 9
10 11
Output
10
Input
5 10
5 7
11 12
16 20
25 26
30 33
Output
18
Input
1 1000000000
1 1000000000
Output
1999999999

题意:给出一个n,h,分别表示有几段上升气流,跳伞时的高度,在上升气流中间时纵坐标不变横坐标增加,没有上升气流时纵坐标减少横坐标增加,问最多横坐标能增加多少

思路:尺取 我们可以想到一定从气流的开头开始跳伞才可能飞得更远,具体看代码

AC code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
#define ll long long
struct gg
{
	ll a,b;
}g[210000];
int main()
{
	ios::sync_with_stdio(0);
	int n,h;
	cin>>n>>h;
	for(int i=1;i<=n;i++)
	  cin>>g[i].a>>g[i].b;
	  ll l=1,r=1,cnt=0,ans=0,kk=0;
	while(r<=n)
	{
		if(r>1)
		{
			kk+=g[r].a-g[r-1].b;//加上间隔
			while(kk>=h)//等于也是不可以的,因为等于表示到达下一段气流时纵坐标为零
			{
				kk-=(g[l+1].a-g[l].b);//减去头端间隔
				 cnt-=(g[l].b-g[l].a);//减去头端上升气流的长度
				 l++;
			}
		}
		cnt+=g[r].b-g[r].a;
		ans=max(ans,cnt);//取最大值
		r++;
	}
	printf("%lld\n",ans+h);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值