CF1168A.Increasing by Modulo

普及+/提高

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Toad Zitz has an array of integers, each integer is between 00 and m−1m-1 inclusive. The integers are a1,a2,…,ana_1, a_2, \ldots, a_n.

In one operation Zitz can choose an integer kk and kk indices i1,i2,…,iki_1, i_2, \ldots, i_k such that 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \leq n. He should then change aija_{i_j} to ((aij+1) mod m)((a_{i_j}+1) \bmod m) for each chosen integer iji_j. The integer mm is fixed for all operations and indices.

Here x mod yx \bmod y denotes the remainder of the division of xx by yy.

Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations.

蟾蜍 Zitz 有一个整数数组,其中每个整数均在 00 到 m−1m-1(含端点)之间。这些整数为 a1,a2,…,ana_1, a_2, \ldots, a_n。

在一次操作中,Zitz 可以选择一个整数 kk 和 kk 个下标 i1,i2,…,iki_1, i_2, \ldots, i_k,满足 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \leq n。然后,他对每个选定的下标 iji_j 将 aija_{i_j} 修改为 ((aij+1) mod m)((a_{i_j}+1) \bmod m)。整数 mm 在所有操作及下标选择中保持不变。

此处 x mod yx \bmod y 表示 xx 除以 yy 所得的余数。

Zitz 希望以最少的操作次数使该数组变为非递减序列。请找出这个最小操作次数。

输入格式

The first line contains two integers nn and mm (1≤n,m≤300 0001 \leq n, m \leq 300\,000) — the number of integers in the array and the parameter mm.

The next line contains nn space-separated integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<m0 \leq a_i \lt m) — the given array.

第一行包含两个整数 nn 和 mm(1≤n,m≤300 0001 \leq n, m \leq 300\,000)—— 分别表示数组中整数的个数和参数 mm。

第二行包含 nn 个用空格分隔的整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<m0 \leq a_i \lt m)—— 给定的数组。

输出格式

Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 00.

It is easy to see that with enough operations Zitz can always make his array non-decreasing.

输出一个整数:Zitz 使其数组变为非递减序列所需的最少操作次数。若无需任何操作,则输出 00。

显然,通过足够多次的操作,Zitz 总能将其数组变为非递减序列。

输入输出样例

  • 输入#1

    5 3
    0 0 0 1 2

    输出#1

    0
  • 输入#2

    5 7
    0 6 1 3 2

    输出#2

    1

说明/提示

In the first example, the array is already non-decreasing, so the answer is 00.

In the second example, you can choose k=2k=2, i1=2i_1 = 2, i2=5i_2 = 5, the array becomes [0,0,1,3,3][0,0,1,3,3]. It is non-decreasing, so the answer is 11.

在第一个例子中,数组已经是非递减的,因此答案为 00。

在第二个例子中,你可以选择 k=2k=2、i1=2i_1 = 2、i2=5i_2 = 5,此时数组变为 [0,0,1,3,3][0,0,1,3,3]。该数组是非递减的,因此答案为 11。

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

首页