CF1168A.Increasing by Modulo

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Toad Zitz has an array of integers, each integer is between 00 and m1m-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 1i1<i2<<ikn1 \leq i_1 < i_2 < \ldots < i_k \leq n . He should then change aija_{i_j} to ((aij+1)modm)((a_{i_j}+1) \bmod m) for each chosen integer iji_j . The integer mm is fixed for all operations and indices.

Here xmodyx \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.

输入格式

The first line contains two integers nn and mm ( 1n,m3000001 \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 ( 0ai<m0 \leq a_i < m ) — the given array.

输出格式

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.

输入输出样例

  • 输入#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 .

首页