E. Icicles

Codeforces
IDCF955E
Time2000ms
Memory256MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back! At the moment the refugee is inside an icy cave with _n_ icicles dangling from the ceiling located in integer coordinates numbered from 1 to _n_. The distance between floor and the _i_\-th icicle is equal to _a__i_. Andrew is free to choose an arbitrary integer point _T_ in range from 1 to _n_ inclusive and at time instant 0 launch a sound wave spreading into both sides (left and right) at the speed of one point per second. Any icicle touched by the wave starts falling at the same speed (that means that in a second the distance from floor to icicle decreases by one but cannot become less that zero). While distance from icicle to floor is more than zero, it is considered passable; as soon as it becomes zero, the icicle blocks the path and prohibits passing. Krakozyabra is initially (i.e. at time instant 0) is located at point and starts running in the right direction at the speed of one point per second. You can assume that events in a single second happen in the following order: first Krakozyabra changes its position, and only then the sound spreads and icicles fall; in particular, that means that if Krakozyabra is currently at point and the falling (i.e. already touched by the sound wave) icicle at point _i_ is 1 point from the floor, then Krakozyabra will pass it and find itself at and only after that the icicle will finally fall and block the path. Krakozyabra is considered entrapped if there are fallen (i.e. with _a__i_ = 0) icicles both to the left and to the right of its current position. Help Andrew find the minimum possible time it takes to entrap Krakozyabra by choosing the optimal value of _T_ or report that this mission is impossible. ## Input The first line contains the number of icicles _n_ (2 ≤ _n_ ≤ 105). The next line contains _n_ space-separated numbers _a__i_ (1 ≤ _a__i_ ≤ 105) — the distances from floor to icicles. ## Output Print an only integer — the minimum time it takes to entrap Krakozyabra between two fallen icicles. If it is impossible, print  - 1. [samples] ## Note In sample case one it's optimal to launch the sound wave from point 3. Then in two seconds icicles 1 and 5 will start falling, and in one more seconds they will block the paths. Krakozyabra will be located at at that time. Note that icicle number 3 will also be fallen, so there will actually be two icicles blocking the path to the left. In sample case two it is optimal to launch the wave from point 2 and entrap Krakozyabra in 2 seconds. In sample case four the answer is impossible.
Andrew 的最爱 Krakozyabra 最近逃跑了,现在他急于把它抓回来! 目前,这只逃亡者位于一个冰洞中,洞顶悬挂着 #cf_span[n] 个冰柱,它们位于从 #cf_span[1] 到 #cf_span[n] 的整数坐标上。第 #cf_span[i] 个冰柱距离地面的高度为 #cf_span[ai]。 Andrew 可以自由选择一个位于范围 #cf_span[1] 到 #cf_span[n](含)内的任意整数点 #cf_span[T],并在时间 #cf_span[0] 发射一个声波,该声波向左右两个方向以每秒一点的速度传播。任何被声波触及的冰柱会以相同的速度开始下落(即每秒距离地面的高度减少 1,但不能小于 0)。当冰柱与地面的距离大于 0 时,该位置被认为是可通行的;一旦距离变为 0,冰柱就会阻断路径,禁止通过。 Krakozyabra 最初(即时间 #cf_span[0])位于点 #cf_span[T],并立即开始以每秒一点的速度向右移动。你可以假设每一秒内事件按以下顺序发生:首先 Krakozyabra 移动其位置,然后声波传播,冰柱才开始下落;特别地,这意味着如果 Krakozyabra 当前位于点 #cf_span[i],而位于点 #cf_span[i] 的冰柱(已被声波触及)距离地面仅剩 1 单位,则 Krakozyabra 将能通过该点并到达 #cf_span[i+1],之后该冰柱才会最终下落并阻断路径。 当 Krakozyabra 当前位置的左侧和右侧都存在已下落(即 #cf_span[ai = 0])的冰柱时,它被视为被困住。请帮助 Andrew 找出通过选择最优的 #cf_span[T] 值,困住 Krakozyabra 所需的最短时间,或者判断该任务不可能完成。 第一行包含冰柱数量 #cf_span[n] #cf_span[(2 ≤ n ≤ 105)]。 第二行包含 #cf_span[n] 个空格分隔的数字 #cf_span[ai] #cf_span[(1 ≤ ai ≤ 105)] —— 即各冰柱距离地面的高度。 请输出一个整数 —— 困住 Krakozyabra 所需的最短时间。如果不可能,输出 #cf_span[ - 1]。 在第一个样例中,最优策略是从点 #cf_span[3] 发射声波。两秒后,冰柱 #cf_span[1] 和 #cf_span[5] 开始下落,再过一秒,它们将完全阻断路径。此时 Krakozyabra 位于点 #cf_span[4]。注意,冰柱 #cf_span[3] 也会下落,因此实际上左侧有两根冰柱阻断路径。 在第二个样例中,最优策略是从点 #cf_span[2] 发射声波,并在 #cf_span[2] 秒后困住 Krakozyabra。 在第四个样例中,答案是不可能完成。 ## Input 第一行包含冰柱数量 #cf_span[n] #cf_span[(2 ≤ n ≤ 105)]。第二行包含 #cf_span[n] 个空格分隔的数字 #cf_span[ai] #cf_span[(1 ≤ ai ≤ 105)] —— 即各冰柱距离地面的高度。 ## Output 请输出一个整数 —— 困住 Krakozyabra 所需的最短时间。如果不可能,输出 #cf_span[ - 1]。 [samples] ## Note 在第一个样例中,最优策略是从点 #cf_span[3] 发射声波。两秒后,冰柱 #cf_span[1] 和 #cf_span[5] 开始下落,再过一秒,它们将完全阻断路径。此时 Krakozyabra 位于点 #cf_span[4]。注意,冰柱 #cf_span[3] 也会下落,因此实际上左侧有两根冰柱阻断路径。 在第二个样例中,最优策略是从点 #cf_span[2] 发射声波,并在 #cf_span[2] 秒后困住 Krakozyabra。 在第四个样例中,答案是不可能完成。
Let $ n $ be the number of icicles, and $ a_i \in \mathbb{Z}^+ $ be the initial height of the icicle at position $ i \in \{1, 2, \dots, n\} $. Andrew chooses a launch point $ T \in \{1, 2, \dots, n\} $ at time $ t = 0 $. A sound wave propagates left and right from $ T $ at speed 1 unit per second. At time $ t $, the wave reaches all positions $ i $ such that $ |i - T| \leq t $. An icicle at position $ i $ begins to fall at time $ |i - T| $, and its height decreases by 1 per second thereafter. Thus, at time $ t \geq |i - T| $, the height of icicle $ i $ is $ \max(0, a_i - (t - |i - T|)) $. The Krakozyabra starts at position $ T $ at time $ t = 0 $ and moves right at speed 1 per second. Thus, at time $ t $, it is at position $ T + t $. An icicle at position $ i $ blocks passage at time $ t $ if its height becomes 0, i.e., if $ a_i - (t - |i - T|) \leq 0 $, or equivalently, $ t \geq a_i + |i - T| $. Krakozyabra is entrapped at time $ t $ if: - There exists some $ i < T + t $ such that $ t \geq a_i + |i - T| $ (an icicle to the left has fallen and blocks), - There exists some $ j > T + t $ such that $ t \geq a_j + |j - T| $ (an icicle to the right has fallen and blocks). We seek the **minimum** $ t \geq 0 $ such that there exists a $ T \in \{1, \dots, n\} $ for which the above two conditions hold simultaneously at time $ t $, and Krakozyabra is at $ T + t \in \{1, \dots, n\} $. Define for each position $ i $ and launch point $ T $, the time at which icicle $ i $ blocks the path: $$ f(i, T) = a_i + |i - T| $$ Then, for fixed $ T $, define: - $ L(T, t) = \exists i \leq T + t - 1 \text{ such that } f(i, T) \leq t $ - $ R(T, t) = \exists j \geq T + t + 1 \text{ such that } f(j, T) \leq t $ We want: $$ \min_{T \in [1, n]} \min \left\{ t \in \mathbb{Z}_{\geq 0} : L(T, t) \land R(T, t) \right\} $$ If no such $ T $ and $ t $ exist, return $ -1 $. --- **Formal Statement:** Given: - $ n \in \mathbb{Z} $, $ 2 \leq n \leq 10^5 $ - $ a = [a_1, a_2, \dots, a_n] \in \mathbb{Z}_{>0}^n $ Define: - $ f(i, T) = a_i + |i - T| $ for $ i, T \in \{1, \dots, n\} $ Find: $$ \min_{T=1}^{n} \min \left\{ t \in \mathbb{N}_0 : \exists i \leq T + t - 1 \text{ s.t. } f(i, T) \leq t \text{ and } \exists j \geq T + t + 1 \text{ s.t. } f(j, T) \leq t \right\} $$ If the set of valid $ (T, t) $ pairs is empty, output $ -1 $.
Samples
Input #1
5
1 4 3 5 1
Output #1
3
Input #2
4
1 2 1 1
Output #2
2
Input #3
2
2 1
Output #3
3
Input #4
2
1 2
Output #4
\-1
API Response (JSON)
{
  "problem": {
    "name": "E. Icicles",
    "description": {
      "content": "Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back! At the moment the refugee is inside an icy cave with _n_ icicles dangling from the ceiling located in integer",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF955E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back!\n\nAt the moment the refugee is inside an icy cave with _n_ icicles dangling from the ceiling located in integer...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Andrew 的最爱 Krakozyabra 最近逃跑了,现在他急于把它抓回来!\n\n目前,这只逃亡者位于一个冰洞中,洞顶悬挂着 #cf_span[n] 个冰柱,它们位于从 #cf_span[1] 到 #cf_span[n] 的整数坐标上。第 #cf_span[i] 个冰柱距离地面的高度为 #cf_span[ai]。\n\nAndrew 可以自由选择一个位于范围 #cf_span[1] 到 #cf_sp...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of icicles, and $ a_i \\in \\mathbb{Z}^+ $ be the initial height of the icicle at position $ i \\in \\{1, 2, \\dots, n\\} $.\n\nAndrew chooses a launch point $ T \\in \\{1, 2, \\dots, n\\}...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments