Submission #1211660

#TimeUsernameProblemLanguageResultExecution timeMemory
1211660abczzGrowing Vegetables is Fun 5 (JOI24_vegetables5)C++20
0 / 100
1744 ms33748 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long

using namespace std;

ll cnt[600000];
ll n, A[600000], B[300000], C[300000];
ll minrnk[600000], maxrnk[600000], G[600000];

vector<ll> solve(ll d, ll w) {
  if (B[0] < A[0]-d || A[0]+d < B[0]) return {};
  vector <ll> ret;
  int j = 0, k = 0;
  vector <ll> X, Y;
  for (int i=0; i<n; ++i) {
    G[i] = -1;
    while (j < n && B[j] < A[i]-d) ++j;
    while (k+1 < n && B[k+1] <= A[i]+d) ++k;
    minrnk[i] = j-i, maxrnk[i] = k-i;
    if (minrnk[i] > 0 && (X.empty() || minrnk[X.back()] < minrnk[i])) X.push_back(i);
    if (Y.empty() || maxrnk[Y.back()] >= maxrnk[i]) Y.push_back(i);
  }
  reverse(X.begin(), X.end());
  j = 0;
  ll s = 0; 
  G[0] = 0;
  //for (int i=0; i<n; ++i) cout << A[i] << " " << B[i] << endl;
  for (int i=n-1; i>=0; --i) {
    //if (!Y.empty()) cout << maxrnk[Y.back()] << " " << Y.back() << " " << s << " " << G[Y.back()] << endl;
    if (X.empty() && (Y.empty() || maxrnk[Y.back()] >= s)) ret.push_back(i);
    if (i) {
      ++s;
      while ((w == 0 ? A[2*n-s] >= A[j] : A[2*n-s] > A[j]) && j <= n-1) {
        ++j;
        G[j] = G[j-1];
      }
      ++G[j];
      while (!X.empty() && (X.back() >= i || (j <= X.back() && minrnk[X.back()] <= s))) X.pop_back();
      while (!Y.empty() && (Y.back() >= i || (Y.back() < j && maxrnk[Y.back()] >= G[Y.back()]))) {
        //cout << Y.back() << " " << j << " " << i-1 << " " << maxrnk[Y.back()] << " " << G[Y.back()] << endl;
        Y.pop_back();
      }
    }
  }
  //for (auto x : ret) cout << x << " ";
  //cout << endl;
  return ret;
}
void swp() {
  for (int i=1; i<n; ++i) swap(A[i], A[2*n-i]);
}
void swp2() {
  for (int i=0; i<n; ++i) swap(B[i], C[i]);
  for (int i=1; i<n/2; ++i) swap(A[i], A[n-i]);
  for (int i=n+1; i<3*n/2; ++i) swap(A[i], A[3*n-i]);
  swap(A[0], A[n]);
  for (int i=0; i<2*n; ++i) A[i] = 1e9-A[i];
  for (int i=0; i<n; ++i) B[i] = 1e9-B[i], C[i] = 1e9-C[i];
  reverse(B, B+n);
  reverse(C, C+n);
}
bool solve2(ll mid) {
  for (int i=0; i<n; ++i) cnt[i] = 0;
  //cout << "a" << endl;
  auto u = solve(mid, 0);
  for (auto x : u) ++cnt[x];
  swp();
  //cout << "b" << endl;
  auto v = solve(mid, 1);
  for (auto x : v) ++cnt[n-1-x];
  swp2();
  //cout << "c" << endl;
  auto g = solve(mid, 0);
  for (auto x : g) ++cnt[x];
  swp();
  //cout << "d" << endl;
  auto h = solve(mid, 1);
  for (auto x : h) ++cnt[n-1-x];
  swp2();
  for (int i=0; i<n; ++i) if (cnt[i] == 4) return 1;
  return 0;
}
int main() {
  cin >> n;
  for (int i=0; i<2*n; ++i) cin >> A[i];
  for (int i=0; i<n; ++i) cin >> B[i];
  for (int i=0; i<n; ++i) cin >> C[i];
  sort(B, B+n);
  sort(C, C+n);
  ll l = 0, r = 1e9, mid;
  /*for (int i=0; i<n; ++i) swap(B[i], C[i]);
  for (int i=0; i<2*n; ++i) cout << A[i] << " ";
  cout << endl;
  for (int i=0; i<n; ++i) cout << B[i] << " ";
  cout << endl;
  for (int i=0; i<n; ++i) cout << C[i] << " ";
  cout << endl;*/
  /*for (int i=0; i<n; ++i) swap(B[i], C[i]);
  solve2(2);
  return 0;*/
  while (l < r) {
    mid = (l+r)/2;
    for (int i=0; i<n; ++i) cnt[i] = 0;
    bool ok = 0;
    if (!solve2(mid)) {
      for (int i=0; i<n; ++i) swap(B[i], C[i]);
      if (solve2(mid)) ok = 1;
      for (int i=0; i<n; ++i) swap(B[i], C[i]);
    }
    else ok = 1;
    if (ok) r = mid;
    else l = mid+1;
  }
  cout << l << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...