personal-training 2

Kefa and Park

Complete Tripartite

Shortest Cycle

Alex and a Rhombus

Nick and Array

Valeriy and Deque

Circle Metro

Pairs

Increasing by Modulo

特别神奇的二分…真的是想不到…果然菜是万恶之源啊!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[333333]; 
int n,m;
int chick(int x)
{
	int pre=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]>pre)
		{
			if(a[i]+x>=m&&(a[i]+x)%m>=pre)
				pre=pre;
			else
				pre=a[i];
		}
		else
		{
			if(a[i]+x>=pre)
				pre=pre;
			else
				return 0;
		}
	}
	return 1;
}
int main()
{	
	cin>>n>>m>>a[0];
	int flag=0;
	int cnt=0;
	int sum=0;
	for(int i=1;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]<a[i-1])
			flag=1;
	}
	if(flag)
	{
		int L=0;
		int R=m;
		int mid;
		while(L<=R)
		{
			mid=(L+R)/2;
			if(chick(mid))
			{
				R=mid-1;
			}
			else
			{
				L=mid+1;
			}
		}
		cout<<L<<endl;	
	}	
	else
	{
		cout<<0<<endl;
	}
	return 0;
}
 

Mr. Kitayuta, the Treasure Hunter

Mr. Kitayuta’s Technology

发布了41 篇原创文章 · 获赞 5 · 访问量 2267

猜你喜欢

转载自blog.csdn.net/mumuhaoshuai/article/details/103302443
今日推荐