参考大佬的博客https://blog.youkuaiyun.com/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);
}
本文针对一道关于跳伞游戏的问题进行了解析,通过尺取法实现了算法设计,旨在求解在给定的上升气流条件下,跳伞者能达到的最大横坐标增加量。代码使用C++实现,展示了具体的实现细节。

403

被折叠的 条评论
为什么被折叠?



