#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
#define oo 100000000000
int n;
long long a[N],T;
bool check2(long long x){
long long r1=min(a[1]+x+T+1,2*T+1),l1=max(-1ll,a[n]-x-1);
// cout<<l1<<' '<<r1<<'\n';
a[0]=-oo;
for(int i=1;i<=n;i++){
// cout<<a[i-1]+T-x<<' '<<a[i]+x<<'\n';
if(a[i]+x+1>a[i-1]+T-x-1+1&&(a[i-1]+T-x<r1)&&(a[i]+x>l1)){
return 1;
}
}
if(a[n]+T-x<r1) return 1;
return 0;
}
bool check1(long long x){
// a[i]-x a[i]+T+x
return a[n]-x<=a[1]+T+x;
}
bool check(long long x){
if(T-1>2*x) return check2(x);
return check1(x);
}
int main(){
freopen("temperature.in","r",stdin);
freopen("temperature.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>T;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]%=T;
}
sort(a+1,a+n+1);
long long l=0,r=T,ans=0;
while(l<=r){
long long mid=l+r>>1;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans;
return 0;
}YUNDOU
这是 2025年10月13日 存档的第 1 版,不是当前版本。查看当前版本