• 【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 OxOx axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.

    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 OxOx axis, covering one unit of distance every second.

    If the glider jumps out at 11, he will stop at 1010. Otherwise, if he jumps out at 22, he will stop at 1212.

    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
    ",ans+h);
    36     return 0;
    37 }

    END

  • 相关阅读:
    history对象 back() forward() go() 和pushState() replaceState()
    计算机组成原理 常用名词解释
    Angular之 Scope和 Directive
    Angular1.x DirtyChecking(脏值检查) $watch, $apply, $digest
    ES6 之 let和const命令 Symbol Promise对象
    Angular1.x 基础总结
    Node.js 常用 API
    前端自动化和前端开发环境
    CSS3入门
    HTML5入门总结 HTML5API
  • 原文地址:https://www.cnblogs.com/antiquality/p/9693523.html
Copyright © 2020-2023  润新知