1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <bits/stdc++.h>
#define ll long long #define ull unsigned long long #define N 500005 #define MOD 998244353 using namespace std;
random_device seed; ranlux48 engine(seed()); int random(int l, int r) { uniform_int_distribution<> distrib(l, r); return distrib(engine); } template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<class t> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
void sol() { ll n, d; cin >> n >> d; vector<ll> a(n+1); for (int i=1; i<=n; i++) { cin >> a[i]; } auto opt = [&](map<ll,ll> itv, ll l, ll m, ll r=-1)->ll { if (--itv[m-l-1] == 0) itv.erase(m-l-1); if (a[n] != m) { if (--itv[r-m-1] == 0) itv.erase(r-m-1); itv[r-l-1]++; } ll t = min(d-(m == a[n]?a[n-1]:a[n])-1, itv.begin()->first); int mx = itv.rbegin()->first; if (--itv[mx] == 0) { itv.erase(mx); } itv[(mx+1)/2-1]++; itv[mx-(mx+1)/2]++; return max(itv.begin()->first, t); }; map<ll,ll> itv; for (int i=1; i<=n; i++) { itv[a[i]-a[i-1]-1]++; } auto [mn, cnt] = *itv.begin(); if (cnt > 2) { cout << mn << "\n"; } else if (cnt == 2) { for (int i=1; i<n; i++) { if (a[i] - a[i-1] - 1 == mn && a[i+1] - a[i] - 1 == mn) { cout << opt(itv, a[i-1], a[i], a[i+1]) << "\n"; return ; } } cout << mn << "\n"; } else { if (a[1]-a[0]-1 == mn) cout << opt(itv, a[0], a[1], a[2]) << "\n"; else if (a[n]-a[n-1]-1 == mn) cout << max(opt(itv, a[n-2], a[n-1], a[n]), opt(itv, a[n-1], a[n])) << "\n"; else for (int i=2; i<n; i++) { if (a[i] - a[i-1] - 1 == mn) { cout << max(opt(itv, a[i-2], a[i-1], a[i]), opt(itv, a[i-1], a[i], a[i+1])) << "\n"; return ; } } } }
int main() { cout << setprecision(15) << fixed; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef SINGLE_INPUT int t; cin >> t; while (t--) { sol(); } #else sol(); #endif return 0; }
|