Question Link
- Question:
A wooden stick with a length of L is supported by N pivot points. Each point is an int value, indicating the distance from the left endpoint of the wooden stick. Split the wooden sticks at those positions so that at least one wooden stick can be dropped and the length of these positions can be output.
3 ≤ L ≤ 10 9 ; 2 ≤ N ≤ 10 5
-
- Analysis:
For the left-side wooden sticks, if they fall down, the center of gravity must be outside the wooden sticks. Two cases: 1, outside the leftmost wooden bar; 2, outside the rightmost wooden bar
You can get an answer range each time, and then process it from right to left. Merge the range into the answer
Const int maxn = 1100000; struct seg {int L, R; bool operator <(const seg & RHs) const {If (L! = RHS. l) return l <RHS. l; return r <RHS. r ;}} X [maxn]; int tot, L, N; int IPT [maxn]; void fun (bool flag) {If (FLAG) X [tot ++] = (SEG) {L-2 * IPT [0], l}; else X [tot ++] = (SEG) {0, 2 * IPT [0]}; rep (I, n-1) {int L = IPT [I] * 2, R = IPT [I + 1]; if (L <= r) {If (FLAG) x [tot ++] = (SEG) {L-R, l-l }; else X [tot ++] = (SEG) {L, r };}} int main () {While (~ Rii (L, n) {tot = 0; rep (I, n) RI (IPT [I]); sort (EPT, IPT + n); fun (false ); rep (I, n) EPT [I] = L-EPT [I]; sort (EPT, EPT + n); fun (true); sort (X, X + ToT); int CNT = 1; FF (I, 1, TOT) {Int & L1 = x [CNT-1]. l, & R1 = x [CNT-1]. r; Int & L2 = x [I]. l, & r2 = x [I]. r; If (r1> = l2) {R1 = max (R1, R2);} else {x [CNT]. L = L2; X [CNT]. R = R2; CNT ++; }}tot = CNT; int ans = 0; rep (I, TOT) {ans + = x [I]. r-X [I]. l;} wi (ANS);} return 0 ;}