Codeforces Round #562 (Div. 2) Increasing by Modulo

http://codeforces.com/contest/1169/problem/C

Looked a long time did not understand the meaning of the questions, then looked at someone else's solution to a problem became clear, was too dishes .......

The number n, each operable to (a [i] +1)% m, the minimum number of seek operations so that does not decrement the number n;

M not operating times with the same operation, the upper limit is m, the lower limit is 0, two points ....

#include <iostream>
using namespace std;
const int maxn=300000+10;
int n,m;
int a[maxn];
int b[maxn];
bool check(int x)
{
    for(int i=1;i<=n;i++)
    b[i]=a[i];
    for(int i=1;i<=n;i++)
    {
        if(b[i]>=b[i-1])
        {
            if(b[i]+x-m>=b[i-1])
            b[i]=b[i-1];
        }
        else if(b[i]+x>=b[i-1])
        b[i]=b[i-1];
        if(b[i]<b[i-1])
        return false;
    }
    /*for(int i=1;i<=n;i++)
    cout<<b[i]<<' ';
    cout<<endl;*/
    return true;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int left=0,right=m,mid,ans=0;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(check(mid))
        {
            ans=mid;
            right=mid-1;
        }
        else 
        {
            left=mid+1;
        }
    }
    cout<<ans<<endl;
    
    return 0;
} 
View Code

 

Guess you like

Origin www.cnblogs.com/lin1874/p/10953776.html