Samurai's stroke

Source: Internet
Author: User

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.