E.Rescheduling the Exam

显然可以二分答案,check里 记录a[i]-a[i-1]-1<x的数量计为nd 计即需要移动的数量
如果nd>=3 则无解
nd =2 这两个需要移动的数要连续
nd=1 去判断是否有可以移动到的位置
nd=0 一定可以
(有一点细节

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
int a[maxn];
bool pd[maxn];
int n,d;
bool ck(int x)
{
	for(int i=1; i<=n; i++) pd[i]=0;
	bool fg=0;
	int nd=0;
	if(a[1]-1<x)
	{
		nd++;
		pd[1]=1;
	}
	if(a[1]-1>2*x)fg=1;
	for(int i=2; i<=n; i++)
	{
		if(a[i]-a[i-1]-1<x)
		{
			pd[i]=1;
			nd++;
		}
		if(a[i]-a[i-1]-1>2*x) fg=1;
	}
	if(a[n+1]-a[n]-1>=x)fg=1;
	if(nd>2)return 0;
	if(nd==0) return 1;
	if(nd==1&&fg)return 1;
	if(nd==1)
	{
		int pos;
		for(int i=1; i<=n; i++)
		{
			if(pd[i])
			{
if(i==n&&a[i]-a[i-2]-1>2*x)return 1;
if(i==n&&a[n+1]-a[n-1]-1>=x)return 1;
				if(i-2>=0&&a[i]-a[i-2]-1>2*x)return 1;
				if(a[i+1]-a[i-1]-1>2*x)return 1;
				//if(i+2<=n+1&&a[i+2]-a[i]-1>2*x)return 1;
				return 0;
			}
		}
	}
	bool fg2=0;
	int pos=-1;
	for(int i=2; i<=n; i++)
	{
		if(pd[i]&&pd[i-1])
		{
			fg2=1;
			pos=i-1;
		}
	}
	if(pos!=-1&&a[pos+1]-a[pos-1]-1<x)return 0;
	if(fg2==0)return 0;
	if(fg)return 1;
if(pos!=-1&&a[pos+1]-a[pos-1]-1>2*x)return 1;
	return 0;
}
signed main()
{
	IOS
	int tt;
	cin>>tt;
	while(tt--)
	{
		cin>>n>>d;
		for(int i=1; i<=n; i++)
		{
			cin>>a[i];
		}
		a[n+1]=d;
		int l=0,r=(int)1e10;
		//cout<<ck(5)<<"\n";
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(ck(mid))l=mid+1;
			else r=mid-1;
		}
 cout<<r<<"\n";
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thusloop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值