A14973 | Sorting Pancakes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Nastya baked $m$ pancakes and spread them on $n$ dishes. The dishes are in a row and numbered from left to right. She put $a_i$ pancakes on the dish with the index $i$ .
Seeing the dishes, Vlad decided to bring order to the stacks and move some pancakes. In one move, he can shift one pancake from any dish to the closest one, that is, select the dish $i$ ( $a_i > 0$ ) and do one of the following:
- if $i > 1$ , put the pancake on a dish with the previous index, after this move $a_i = a_i - 1$ and $a_{i - 1} = a_{i - 1} + 1$ ;
- if $i < n$ , put the pancake on a dish with the following index, after this move $a_i = a_i - 1$ and $a_{i + 1} = a_{i + 1} + 1$ .
Vlad wants to make the array $a$ non-increasing, after moving as few pancakes as possible. Help him find the minimum number of moves needed for this.
The array $a=[a_1, a_2,\dots,a_n]$ is called non-increasing if $a_i \ge a_{i+1}$ for all $i$ from $1$ to $n-1$ .
Seeing the dishes, Vlad decided to bring order to the stacks and move some pancakes. In one move, he can shift one pancake from any dish to the closest one, that is, select the dish $i$ ( $a_i > 0$ ) and do one of the following:
- if $i > 1$ , put the pancake on a dish with the previous index, after this move $a_i = a_i - 1$ and $a_{i - 1} = a_{i - 1} + 1$ ;
- if $i < n$ , put the pancake on a dish with the following index, after this move $a_i = a_i - 1$ and $a_{i + 1} = a_{i + 1} + 1$ .
Vlad wants to make the array $a$ non-increasing, after moving as few pancakes as possible. Help him find the minimum number of moves needed for this.
The array $a=[a_1, a_2,\dots,a_n]$ is called non-increasing if $a_i \ge a_{i+1}$ for all $i$ from $1$ to $n-1$ .
输入格式
The first line of the input contains two numbers $n$ and $m$ ( $1 \le n, m \le 250$ ) — the number of dishes and the number of pancakes, respectively.
The second line contains $n$ numbers $a_i$ ( $0 \le a_i \le m$ ), the sum of all $a_i$ is equal to $m$ .
The second line contains $n$ numbers $a_i$ ( $0 \le a_i \le m$ ), the sum of all $a_i$ is equal to $m$ .
输出格式
Print a single number: the minimum number of moves required to make the array $a$ non-increasing.
输入输出样例
输入 #1
6 19 5 3 2 3 3 3
输出 #1
2
输入 #2
3 6 3 2 1
输出 #2
0
输入 #3
3 6 2 1 3
输出 #3
1
输入 #4
6 19 3 4 3 3 5 1
输出 #4
3
输入 #5
10 1 0 0 0 0 0 0 0 0 0 1
输出 #5
9
In the first example, you first need to move the pancake from the dish $1$ , after which $a = [4, 4, 2, 3, 3, 3]$ . After that, you need to move the pancake from the dish $2$ to the dish $3$ and the array will become non-growing $a = [4, 3, 3, 3, 3, 3]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted