Codeforces 1041D Glider

题面在这里

显然蓝色部分可以缩成一条竖线,权值为区间长度

那么就相当于做一个长度为\(h\)的滑动窗口

示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn=200005;
int n,h,a[maxn],w[maxn];
int main(){
scanf("%d%d",&n,&h);
int s=0;
for (int i=1;i<=n;i++){
int l,r;scanf("%d%d",&l,&r);
l-=s;r-=s;
s+=r-l; a[i]=l;w[i]=r-l;
}
int ans=0;s=0;
for (int i=1,j=1;i<=n;i++){
while (j<=n&&a[j]<a[i]+h) s+=w[j++];
ans=max(ans,s);
s-=w[i];
}
printf("%d",ans+h);
return 0;
}