Codforces1169C.Increasing by Modulo 二分

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

#include
using namespace std;
typedef long long ll;
typedef pair pii;	//1e9
typedef pair pll;
const int maxn=4e5+5;
const int INF=0x3f3f3f3f;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define forn(i,n) for(int i=0;ib[i]+mid)
			return false;
		else if( (b[i]+mid)>=m && (b[i]+mid)%m >=b[i-1] )	//构造递增 b[i]+mid后肯定比 b[i-1]大 顶多到m-1 
		//无论递增递减说明值有点大了 超过m 
			b[i]=b[i-1];	//尽量小 0 6 1 mid=2 m=7
		//2 1 mid=4 m=3 
		else if(b[i-1]>b[i] && b[i-1]<=b[i]+mid)
			b[i]=b[i-1];
		else
			b[i]=a[i];		 
	}
	return true;
} 
int main()
{
	cin>>n>>m;
	rep(i,1,n){
		cin>>a[i];
		
	}
	ll l=0,r=m,ans=0;
	while(l<=r){
		ll mid=(l+r)>>1;
		if(check(mid))
		{
			ans=mid;
			r=mid-1;	//够 +1次数可以少些 
		}	
		else
			l=mid+1;
	}
	return 0;
}

你可能感兴趣的:(二分)