【two pointers 細節題】cf1041dD. Glider
像這樣細節老是打掛不行啊……
A plane is flying at a constant height of hh meters above the ground surface. Let‘s consider that it is flying from the point (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx axis.
A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let‘s consider that he may start only when the plane‘s coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to
There are ascending air flows on certain segments, each such segment is characterized by two numbers x1x1 and x2x2 (x1<x2x1<x2) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn‘t descend, so his second coordinate stays the same each second. The glider still flies along
Determine the maximum distance along OxOx axis from the point where the glider‘s flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is 0.
Input
The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(1≤n≤2⋅105,1≤h≤109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.
Each of the next nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤109) — the endpoints of the ii-th ascending air flow segment. No two segments intersect, and they are given in ascending order.
Output
Print one integer — the maximum distance along OxOx axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.
題目大意
二維坐標系裏有一些藍色區域和白色區域。在藍色區域中飛行高度不會降低;在白色區域中飛行每個單位會降低一個單位。
請找出一條路徑使得水平飛行距離最遠。
題目分析
可以看成是這個東西:
那麽相當於是在選取的ci≤h的情況下,使Σwi最大。
於是做法還是挺顯然的,因為對於每個起點,選取的[l,r]是單調的。
關鍵在於邊界條件的小細節……今天做的時候WA了兩發。
1 #include<bits/stdc++.h> 2 const int maxn = 200035; 3 4 int n,h,l,r,lim,ans,cnt; 5 int ls[maxn],rs[maxn]; 6 int w[maxn],c[maxn]; 7 8 int read() 9 { 10 char ch = getchar(); 11 int num = 0; 12 bool fl = 0; 13 for (; !isdigit(ch); ch = getchar()) 14 if (ch==‘-‘) fl = 1; 15 for (; isdigit(ch); ch = getchar()) 16 num = (num<<1)+(num<<3)+ch-48; 17 if (fl) num = -num; 18 return num; 19 } 20 int main() 21 { 22 n = read(), h = read(); 23 for (int i=1; i<=n; i++) ls[i] = read(), rs[i] = read(); 24 for (int i=1; i<=n; i++) 25 w[i] = rs[i]-ls[i], c[i] = ls[i+1]-rs[i]; 26 r = 1, cnt = ans = w[1]; 27 for (int i=1; i<=n; i++) 28 { 29 while (r<n&&lim+c[r]<h) lim += c[r++], cnt += w[r]; 30 ans = std::max(ans, cnt); 31 if (r!=i) lim -= c[i]; //向前推一格 32 else cnt += w[i+1], r = i+1; //間隙太大,置零重開 33 cnt -= w[i]; //減去當前位置 34 } 35 printf("%d\n",ans+h); 36 return 0; 37 }
END
【two pointers 細節題】cf1041dD. Glider