题目 https://codeforces.com/problemset/problem/1168/A
题意: 给n个数字,可以任选一些数字+1,在对m取模的情况下,问最少需要操作几次使得数列变为不下降序列
二分操作次数, 验证的时候贪心构造当前序列,是ai尽可能等于ai-1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=300005;
int a[maxn];
int n ,m;
int ok(int x)
{
int la=0,tmp;
for(int i =0;i<n;i++)
{
tmp=a[i];
if(tmp+x<m)
{
if(tmp<=la&&(tmp+x)>=la)
tmp=la;
//cout<<a[i]<<endl;
}
else
{
if((tmp+x)%m<la&&la<(tmp))
tmp=tmp;
else tmp=la;
}
//cout<<x<<" "<<i<<" "<<a[i]<<endl;
if(tmp<la) return 0;
la=tmp;
}
return 1;
}
int main()
{
ios::sync_with_stdio(0);
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l =0,r=m-1;
while(l<r)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
if(ok(mid)) r=mid;
else l=mid+1;
}
cout<<r<<endl;
return 0;
}