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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章