尺取法之老年康复
codeforces 1041D Glider 题目题目:戳这里
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 2e5 + 10; 5 struct nod 6 { 7 ll x1; 8 ll x2; 9 ll cost; 10 }nu[maxn]; 11 int main() { 12 int n; 13 ll h; 14 scanf("%d %lld", &n, &h); 15 for(int i = 1; i <= n; ++i) 16 { 17 scanf("%lld %lld", &nu[i].x1, &nu[i].x2); 18 } 19 nu[n + 1].x1 = 3e9; 20 for(int i = 1; i <= n; ++i) 21 { 22 nu[i].cost = nu[i + 1].x1 - nu[i].x2; 23 } 24 ll ad = 0; 25 ll sum = 0; 26 ll ans = 0; 27 int r= 0; 28 int l = 0; 29 //尺取 30 while(l <= r && r < n) 31 { 32 if(sum >= h) 33 { 34 ++l; 35 ad -= nu[l].x2 - nu[l].x1; 36 sum -= nu[l].cost; 37 } 38 else 39 { 40 ++r; 41 ad += nu[r].x2 - nu[r].x1; 42 sum += nu[r].cost; 43 } 44 ans = max(ans, ad + h); 45 } 46 printf("%lld\n", ans); 47 return 0; 48 }