luogu#P12193. [NOISG 2025 Prelim] Ducks And Buttons

[NOISG 2025 Prelim] Ducks And Buttons

题目描述

Warning for C++ users: The large size of integers involved in this problem may require the use of the long long data type instead of the int data type.

Shor the Duck is playing a game with his friends! The game is played on a 1-dimensional grid consisting of nn cells in, arranged in a row and numbered from 11 to nn from left to right.

Each cell has a button. The button on cell ii will be permanently pressed if at any point in time there are at least a[i]a[i] ducks on that cell. Even if the ducks leave, the button remains pressed. To win the game, all nn buttons must be pressed.

Initially, there are dd ducks on cell 11. In one move, a single duck can move one cell left or right.

Determine the minimum total number of moves required to win the game. It is guaranteed that it is possible to win the game with some number of moves.

输入格式

Your program must read from standard input.

The first line of input contains two space-separated integers nn and dd.

The second line of input contains nn space-separated integers a[1],a[2],,a[n]a[1], a[2], \ldots, a[n].

输出格式

Your program must print to standard output.

Output a single integer, the minimum total number of moves required to win the game.

2 199
175 42
42
5 3
1 1 0 1 0
3
5 7
2 2 2 2 2
8
7 5
1 3 3 4 5 5 5
30
8 9
7 6 6 6 3 3 3 1
28
8 5
2 3 5 1 4 2 1 0
21
4 1000000000
1 1 1 999999999
2999999997

提示

Subtasks

For all test cases, the input will satisfy the following bounds:

  • 1n2000001 \leq n \leq 200\,000
  • 1d1091 \leq d \leq 10^9
  • 0a[i]d0 \leq a[i] \leq d for all 1in1 \leq i \leq n
  • It is possible to win the game with some number of moves.

Your program will be tested on input instances that satisfy the following restrictions:

Subtask Marks Additional Constraints
00 Sample test cases
11 88 n=2n = 2
22 55 a[i]=0a[i] = 0
33 1111 a[i]1a[i] \leq 1
44 66 All values of a[i]a[i] are equal
55 1919 n,d1000n, d \leq 1000
66 1212 a[i]a[i] is non-decreasing
77 1616 a[i]a[i] is non-increasing
88 2323 No additional constraints

Sample Test Case 1 Explanation

This test case is valid for subtasks 1,5,71, 5, 7, and 88.

Sample Test Case 2 Explanation

This test case is valid for subtasks 3,53, 5, and 88.

Sample Test Case 3 Explanation

This test case is valid for subtasks 4,5,6,74, 5, 6, 7, and 88.

Sample Test Case 4 Explanation

This test case is valid for subtasks 5,65, 6, and 88.

Sample Test Case 5 Explanation

This test case is valid for subtasks 5,75, 7, and 88.

Sample Test Case 6 Explanation

This test case is valid for subtasks 55 and 88.

One possible sequence of moves that minimises the total number of moves is shown above. Each red arrow is a move, and the number above it indicates the order of the moves, with move 11 coming first.

  • Button 11 is first pressed before any moves occur.
  • Button 22 is first pressed after move 33.
  • Button 33 is first pressed after move 1010.
  • Button 44 is first pressed after move 1111.
  • Button 55 is first pressed after move 1818.
  • Button 66 is first pressed after move 2020.
  • Button 77 is first pressed after move 2121.
  • Button 88 is first pressed before any moves occur (since a[8]=0a[8] = 0).

Since all buttons are pressed by the end of all 2121 moves, 2121 moves is sufficient to win the game. It can be proven that 2121 moves is the minimum total number of moves needed.

Sample Test Case 7 Explanation

This test case is valid for subtasks 4,64, 6, and 88.