/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n;
ll t, a[N];
ll get(ll x,ll y){
if(x>y) swap(x,y);
return min((y-x+1)/2, (x+t-y+1)/2);
}
void solve(){
cin >> n >> t;
for(int i = 1; i <= n; ++i){
cin >> a[i];
a[i] %= t;
}
sort(a+1, a+1+n);
ll ans = 1e18;
for(int i = 1; i <= n; ++i){
// cout << a[i] << '\n';
int L = 1, R = i, lgo = i;
while(L <= R){
int m = L+R>>1;
if(a[m] < a[i] - t/2){
L = m + 1;
}else R = m - 1, lgo = m;
}
L = i, R = n; int rgo = i;
while(L <= R){
int m = L+R>>1;
if(a[m] > a[i] + t/2){
R = m - 1;
}else L = m + 1, rgo = m;
}
vector<ll> v;
v.pb(a[lgo]);
v.pb(a[rgo]);
if(lgo>1) v.pb(a[lgo - 1]);
if(lgo>2) v.pb(a[lgo - 2]);
if(rgo<n) v.pb(a[rgo + 1]);
if(rgo<n-1) v.pb(a[rgo + 2]);
ll mx = 0;
for(int x: v) for(int y: v) mx=max(mx,get(x,y));
for(ll x: v) mx=max(mx,min(abs(a[i]-x), (x < a[i] ? x + t - a[i] : a[i] + t - x)));
ans=min(ans,mx);
// cout << lgo << ' ' << rgo << '\n';
// ans = min(ans, a[i] * (i - lgo) - (pref[i - 1] - pref[lgo - 1]) + (suf[i + 1] - suf[rgo + 1]) - a[i] * (rgo - i) +
// (lgo - 1) * t + pref[lgo - 1] - a[i] * (lgo - 1) + (n - rgo) * t - suf[rgo + 1] + a[i] * (n - rgo));
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |