CF1675G.Sorting Pancakes

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Nastya baked mm pancakes and spread them on nn dishes. The dishes are in a row and numbered from left to right. She put aia_i pancakes on the dish with the index ii.

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 ii (ai>0a_i \gt 0) and do one of the following:

  • if i>1i \gt 1, put the pancake on a dish with the previous index, after this move ai=ai−1a_i = a_i - 1 and ai−1=ai−1+1a_{i - 1} = a_{i - 1} + 1;
  • if i<ni \lt n, put the pancake on a dish with the following index, after this move ai=ai−1a_i = a_i - 1 and ai+1=ai+1+1a_{i + 1} = a_{i + 1} + 1.

Vlad wants to make the array aanon-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]a=[a_1, a_2,\dots,a_n] is called non-increasing if ai≥ai+1a_i \ge a_{i+1} for all ii from 11 to n−1n-1.

娜斯佳烤了 mm 个煎饼,并将它们分放在 nn 个盘子上。这些盘子排成一行,从左到右依次编号。她在编号为 ii 的盘子上放了 aia_i 个煎饼。

弗拉德看到这些盘子后,决定对煎饼堆进行整理,移动其中一些煎饼。在一次操作中,他可以将一个煎饼从任意一个盘子移动到与其相邻的盘子上,即:选择一个满足 ai>0a_i > 0 的盘子 ii,并执行以下两种操作之一:

  • 若 i>1i > 1,则将该煎饼移到前一个编号的盘子上;操作后有 ai=ai−1a_i = a_i - 1 且 ai−1=ai−1+1a_{i - 1} = a_{i - 1} + 1;
  • 若 i<ni < n,则将该煎饼移到后一个编号的盘子上;操作后有 ai=ai−1a_i = a_i - 1 且 ai+1=ai+1+1a_{i + 1} = a_{i + 1} + 1。

弗拉德希望经过尽可能少的操作次数,使得数组 aa 变为非递增序列。请你帮他求出达成这一目标所需的最少操作次数。

数组 a=[a1,a2,…,an]a=[a_1, a_2,\dots,a_n] 称为非递增的,当且仅当对所有 i=1,2,…,n−1i = 1, 2, \dots, n-1,均满足 ai≥ai+1a_i \ge a_{i+1}。

输入格式

The first line of the input contains two numbers nn and mm (1≤n,m≤2501 \le n, m \le 250) — the number of dishes and the number of pancakes, respectively.

The second line contains nn numbers aia_i (0≤ai≤m0 \le a_i \le m), the sum of all aia_i is equal to mm.

输入的第一行包含两个数字 nn 和 mm(1≤n,m≤2501 \le n, m \le 250)——分别表示菜肴的数量和煎饼的总数。

第二行包含 nn 个数字 aia_i(0≤ai≤m0 \le a_i \le m),所有 aia_i 的总和等于 mm。

输出格式

Print a single number: the minimum number of moves required to make the array aa non-increasing.

输出一个整数:使数组 aa 变为非递增序列所需的最少操作次数。

输入输出样例

  • 输入#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 11, after which a=[4,4,2,3,3,3]a = [4, 4, 2, 3, 3, 3]. After that, you need to move the pancake from the dish 22 to the dish 33 and the array will become non-growing a=[4,3,3,3,3,3]a = [4, 3, 3, 3, 3, 3].

在第一个例子中,你需要先将盘子 11 上的煎饼移走,之后数组变为 a=[4,4,2,3,3,3]a = [4, 4, 2, 3, 3, 3]。接着,你需要将盘子 22 上的煎饼移到盘子 33 上,此时数组将变为非递增序列 a=[4,3,3,3,3,3]a = [4, 3, 3, 3, 3, 3]。

输入解题思路,AI测评打分。不知道怎么写?

首页