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 cells in, arranged in a row and numbered from to from left to right.
Each cell has a button. The button on cell will be permanently pressed if at any point in time there are at least ducks on that cell. Even if the ducks leave, the button remains pressed. To win the game, all buttons must be pressed.
Initially, there are ducks on cell . 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 and .
The second line of input contains space-separated integers .
输出格式
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:
- for all
- 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 |
---|---|---|
Sample test cases | ||
All values of are equal | ||
is non-decreasing | ||
is non-increasing | ||
No additional constraints |
Sample Test Case 1 Explanation
This test case is valid for subtasks , and .
Sample Test Case 2 Explanation
This test case is valid for subtasks , and .
Sample Test Case 3 Explanation
This test case is valid for subtasks , and .
Sample Test Case 4 Explanation
This test case is valid for subtasks , and .
Sample Test Case 5 Explanation
This test case is valid for subtasks , and .
Sample Test Case 6 Explanation
This test case is valid for subtasks and .
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 coming first.
- Button is first pressed before any moves occur.
- Button is first pressed after move .
- Button is first pressed after move .
- Button is first pressed after move .
- Button is first pressed after move .
- Button is first pressed after move .
- Button is first pressed after move .
- Button is first pressed before any moves occur (since ).
Since all buttons are pressed by the end of all moves, moves is sufficient to win the game. It can be proven that moves is the minimum total number of moves needed.
Sample Test Case 7 Explanation
This test case is valid for subtasks , and .