CF1675G.Sorting Pancakes
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Nastya baked m pancakes and spread them on n dishes. The dishes are in a row and numbered from left to right. She put ai 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 (ai>0) and do one of the following:
- if i>1, put the pancake on a dish with the previous index, after this move ai=ai−1 and ai−1=ai−1+1;
- if i<n, put the pancake on a dish with the following index, after this move ai=ai−1 and ai+1=ai+1+1.
Vlad wants to make the array anon-increasing, after moving as few pancakes as possible. Help him find the minimum number of moves needed for this.
The array a=[a1,a2,…,an] is called non-increasing if ai≥ai+1 for all i from 1 to n−1.
娜斯佳烤了 m 个煎饼,并将它们分放在 n 个盘子上。这些盘子排成一行,从左到右依次编号。她在编号为 i 的盘子上放了 ai 个煎饼。
弗拉德看到这些盘子后,决定对煎饼堆进行整理,移动其中一些煎饼。在一次操作中,他可以将一个煎饼从任意一个盘子移动到与其相邻的盘子上,即:选择一个满足 ai>0 的盘子 i,并执行以下两种操作之一:
- 若 i>1,则将该煎饼移到前一个编号的盘子上;操作后有 ai=ai−1 且 ai−1=ai−1+1;
- 若 i<n,则将该煎饼移到后一个编号的盘子上;操作后有 ai=ai−1 且 ai+1=ai+1+1。
弗拉德希望经过尽可能少的操作次数,使得数组 a 变为非递增序列。请你帮他求出达成这一目标所需的最少操作次数。
数组 a=[a1,a2,…,an] 称为非递增的,当且仅当对所有 i=1,2,…,n−1,均满足 ai≥ai+1。
输入格式
The first line of the input contains two numbers n and m (1≤n,m≤250) — the number of dishes and the number of pancakes, respectively.
The second line contains n numbers ai (0≤ai≤m), the sum of all ai is equal to m.
输入的第一行包含两个数字 n 和 m(1≤n,m≤250)——分别表示菜肴的数量和煎饼的总数。
第二行包含 n 个数字 ai(0≤ai≤m),所有 ai 的总和等于 m。
输出格式
Print a single number: the minimum number of moves required to make the array a non-increasing.
输出一个整数:使数组 a 变为非递增序列所需的最少操作次数。
输入输出样例
输入#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].
在第一个例子中,你需要先将盘子 1 上的煎饼移走,之后数组变为 a=[4,4,2,3,3,3]。接着,你需要将盘子 2 上的煎饼移到盘子 3 上,此时数组将变为非递增序列 a=[4,3,3,3,3,3]。
输入解题思路,AI测评打分。不知道怎么写?