http://codeforces.com/problemset/problem/1168/A
Effect: an array of a modulo m, one operation can make a plurality of array elements (ai + 1)% m, let the whole array is not reduced, the minimum number of seek operations
Ideas: Easy to know the answer up to no greater than x modulo m, we can binary search x. If the current solution feasible, then the search space is reduced to [l, mid], infeasible (x> mid) is reduced to [MID, r] up until l = r-1, r at this time is the minimum operand x .
Note (lf <= prev + m && prev + m <= rf) meaning it represents the interval [lf, rf] has more than a certain period of m, the interval after each new point mod m intervals, whether obtained It contains prev
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=3e5+5; int a[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); //freopen("datain.txt","r",stdin); int n,m; cin>>n>>m; for(int i=0;i<n;i++){ cin>>a[i]; } int l=-1,r=m; the while (L <R- 1 ) { int MID = (L + R & lt) / 2 ; int PREV = 0 ; // where the prev = a [0], for from 1 to start the cycle will lead WA BOOL Bad = to false ; // since this approach to a [0] as a fixed value, is not contained in the binary search for ( int I = 0 ; I <n-; I ++) { // but obviously some optimal solution requires modification a [ 0] int LF = a [I], RF = a [I] + MID; // For example, some cases of i = 0 to two assigned to the right part of the initial section IF ((LF <= PREV && PREV <= RF) || (LF <= m + && PREV PREV + m <= RF)) { Continue ; } if(lf<prev){ bad=true; break; } else{ prev=lf; } } if(bad) l=mid; else r=mid; } cout<<r<<endl; }