답안 #1074503

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1074503 2024-08-25T10:51:08 Z Zanite Growing Vegetables is Fun 5 (JOI24_vegetables5) C++17
100 / 100
3997 ms 62672 KB
// header file
#include <bits/stdc++.h>
// pragma
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// macros
#define endl "\n"
#define ll long long
#define mp make_pair
#define ins insert
#define lb lower_bound
#define pb push_back
#define ub upper_bound
#define lll __int128
#define fi first
#define se second
using namespace std;
const int lim = 6e5 + 5;
namespace fenwick {
  int a[lim];
  void update(int idx, int val) {
    // cerr << idx << endl;
    while(idx < lim) {
      a[idx] += val;
      idx += idx&-idx;
    }
  }
  void update(int l, int r, int val) {
    if(l > r) return;
    for (; l < lim; l += (l & -l)) a[l] += val;
    r++;
    for (; r < lim; r += (r & -r)) a[r] -= val;
  }
  int query(int idx) {
    if(idx >= lim)
      idx = lim - 1;
    int res = 0;
    for (; idx > 0; idx -= (idx & -idx)) res += a[idx];
    return res;
  }
};
struct disjoint_setr {
  int par[lim], sz[lim], h[lim];
  void reset() {
    for (int i = 0; i < lim; i++) {
      par[i] = h[i] = i;
      sz[i] = 1;
    }
  }
  disjoint_setr() {
    reset();
  }
  int rep(int x) {
    return par[x] == x ? x : par[x] = rep(par[x]);
  }
  int nxt(int x) { return h[rep(x)]; }
  void erase(int x) { merge(x, x + 1); }
  void merge(int x, int y) {
    x = rep(x), y = rep(y);
    if(x != y) {
      if (sz[x] < sz[y]) swap(x, y);
      par[y] = x;
      h[x] = max(h[x], h[y]);
      sz[x] += sz[y];
    }
  }
} redr, bluer;
struct disjoint_setl {
  int par[lim], sz[lim], h[lim];
  void reset() {
    for (int i = 0; i < lim; i++) {
      par[i] = h[i] = i;
      sz[i] = 1;
    }
  }
  disjoint_setl() {
    reset();
  }
  int rep(int x) {
    return par[x] == x ? x : par[x] = rep(par[x]);
  }
  int prv(int x) { return h[rep(x)]; }
  void erase(int x) { merge(x - 1, x); }
  void merge(int x, int y) {
    x = rep(x), y = rep(y);
    if(x != y) {
      if (sz[x] < sz[y]) swap(x, y);
      par[y] = x;
      h[x] = min(h[x], h[y]);
      sz[x] += sz[y];
    }
  }
} redl, bluel;
int main() {
  ios_base::sync_with_stdio(0); cin.tie(NULL);
  int n;
  cin >> n;
  int a[2 * n + 5];
  for(int i = 1; i <= 2 * n; ++i)
    cin >> a[i];
  int b[n + 5], c[n + 5];
  for(int i = 1; i <= n; ++i)
    cin >> b[i];
  for(int i = 1; i <= n; ++i)
    cin >> c[i];
  sort(b + 1, b + n + 1);
  sort(c + 1, c + n + 1);
  int l = 0, r = 1e9, res = -1;
  vector<pair<int, int>> v;
  for(int i = 1; i <= 2 * n; ++i)
    v.pb(mp(a[i], i));
  sort(v.begin(), v.end());
  pair<int, int> validb[2 * n + 5], validr[2 * n + 5];
  while(l <= r) {
    memset(fenwick::a, 0, sizeof(fenwick::a));
    redl.reset();
    redr.reset();
    bluel.reset();
    bluer.reset();
    int mid = (l + r) >> 1;
    // max diff -> mid
    // try each partition what is the max diff
    // nanti ada banyak validity test, tinggal cek validity testnya mana aja
    int b1 = 1, b2 = 1, r1 = 1, r2 = 1;
    for(auto p : v) {
      // cerr << p.fi << " " << mid << " " << b[b1] << endl;
      while(b1 <= 2 * n && p.fi - mid > b[b1])
        ++b1;
      while(b2 <= 2 * n && p.fi + mid >= b[b2])
        ++b2;
      while(r1 <= 2 * n && p.fi - mid > c[r1])
        ++r1;
      while(r2 <= 2 * n && p.fi + mid >= c[r2])
        ++r2;
      validb[p.se] = mp(b1, b2 - 1);
      validr[p.se] = mp(r1, r2 - 1);
      // cerr << b1 << " " << b2 << " " << r1 << " " << r2 << endl;
    }
    // cerr << "TEST" << endl;
    for(auto p : v) {
      // process
      // cerr << "UPDATE" << endl;
      fenwick::update(max(1, p.se - n + 1), p.se, 1);
      // observe that blue on left/right of that segment can be invalid
      int idx = p.se;
      // cerr << "TEST" << endl;
      int val;
      while((val = bluer.nxt(max(1, p.se - n + 1))) <= p.se && val > 0) {
        int tmp2 = fenwick::query(val);
        if(tmp2 < validb[idx].fi || tmp2 > validb[idx].se)
          bluer.erase(val), bluel.erase(val);
        else
          break;
      }
      while((val = bluel.prv(p.se)) >= max(1, p.se - n + 1)) {
        int tmp2 = fenwick::query(val);
        if(tmp2 < validb[idx].fi || tmp2 > validb[idx].se)
          bluer.erase(val), bluel.erase(val);
        else
          break;
      }
      // cerr << "DONE" << endl;
      if(p.se <= n) {
        fenwick::update(p.se + n + 1, 2 * n, 1);
        // observe that blue on left/right of that segment can be invalid
        while((val = bluer.nxt(p.se + n + 1)) <= 2 * n) {
          // cerr << val << endl;
          int tmp2 = fenwick::query(val);
          // cerr << "AFTER" << endl;
          if(tmp2 < validb[idx].fi || tmp2 > validb[idx].se) {
            bluer.erase(val), bluel.erase(val);
            // cerr << "HERE" << endl;
          }
          else
            break;
        }
        // cerr << "CHECK" << endl;
        while((val = bluel.prv(2 * n)) >= p.se + n + 1) {
          // cerr << val << endl;
          int tmp2 = fenwick::query(val);
          if(tmp2 < validb[idx].fi || tmp2 > validb[idx].se)
            bluer.erase(val), bluel.erase(val);
          else
            break;
        } 
      }
      // cerr << "TEST2" << endl;
      while((val = redr.nxt(max(1, p.se - n + 1))) <= p.se && val > 0) {
        // cerr << "check " << *it << " due to " << p.se << " " << fenwick::query(*it) << " " << validr[idx].fi << endl; 
        int tmp2 = fenwick::query(val);
        if(tmp2 < validr[idx].fi || tmp2 > validr[idx].se)
          redr.erase(val), redl.erase(val);
        else
          break;
      }
      while((val = redl.prv(p.se)) >= max(1, p.se - n + 1)) {
        // cerr << "check " << *it << " due to " << p.se << " " << fenwick::query(*it) << " " << validr[idx].se << endl; 
        int tmp2 = fenwick::query(val);
        if(tmp2 < validr[idx].fi || tmp2 > validr[idx].se)
          redr.erase(val), redl.erase(val);
        else
          break;
      }
      if(p.se <= n) {
        // observe that red on left/right of that segment can be invalid
        while((val = redr.nxt(p.se + n + 1)) <= 2 * n) {
          // cerr << "check " << *it << " due to " << p.se << " " << fenwick::query(*it) << " " << validr[idx].fi << endl; 
          int tmp2 = fenwick::query(val);
          if(tmp2 < validr[idx].fi || tmp2 > validr[idx].se)
            redr.erase(val), redl.erase(val);
          else
            break;
        }
        while((val = redl.prv(2 * n)) >= p.se + n + 1) {
          // cerr << "check " << *it << " due to " << p.se << " " << fenwick::query(*it) << " " << validr[idx].se << endl; 
          int tmp2 = fenwick::query(val);
          if(tmp2 < validr[idx].fi || tmp2 > validr[idx].se)
            redr.erase(val), redl.erase(val);
          else
            break;
        } 
      }
      // cerr << "FINISH" << endl;
    }
    // blue and red have to complement each other
    bool ans = 0;
    // cerr << "MID IS " << mid << endl;
    for(int i = 1; i + n <= 2 * n; ++i) {
      // cerr << i << " " << i + n << endl;
      // cerr << bluel.prv(i) << " " << bluel.prv(i + n) << endl;
      // cerr << redl.prv(i) << " " << redl.prv(i + n) << endl;
      if((bluel.prv(i) == i && redl.prv(i + n) == i + n) || (redl.prv(i) == i && bluel.prv(i + n) == i + n))
        ans = 1;
      // cerr << "DONE" << endl;
    }
    /*
    if(mid <= 20) {
      cerr << "DEBUG " << mid << endl;
      for(auto x : red) {
        cerr << x << " ";
      }
      cerr << endl;
      for(auto x : blue) {
        cerr << x << " ";
      }
      cerr << endl;
    }
    */
    if(ans) 
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  cout << res << endl;
  // choose a contiguous segment L to R such that we use one color
  // N^2 approach -> pair greedily (sorted)
  // int res = 1e9;
  // for(int i = 1; i + n <= 2 * n + 1; ++i) {
  //   vector<int> blue, red;
  //   for(int j = 1; j < i; ++j) {
  //     blue.pb(a[j]);
  //   }
  //   for(int j = i; j < i + n; ++j) {
  //     red.pb(a[j]);
  //   }
  //   for(int j = i + n; j <= 2 * n; ++j) {
  //     blue.pb(a[j]);
  //   }
  //   sort(blue.begin(), blue.end());
  //   sort(red.begin(), red.end());
  //   int mx = 0;
  //   for(int k = 1; k <= n; ++k) {
  //     mx = max({mx, abs(blue[k - 1] - b[k]), abs(red[k - 1] - c[k])});
  //   }
  //   res = min(res, mx);
  //   mx = 0;
  //   swap(red, blue);
  //   for(int k = 1; k <= n; ++k) {
  //     mx = max({mx, abs(blue[k - 1] - b[k]), abs(red[k - 1] - c[k])});
  //   }
  //   res = min(res, mx);
  // }
  // cout << res << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 30812 KB Output is correct
2 Correct 36 ms 30808 KB Output is correct
3 Correct 36 ms 30812 KB Output is correct
4 Correct 42 ms 30808 KB Output is correct
5 Correct 37 ms 30812 KB Output is correct
6 Correct 36 ms 30812 KB Output is correct
7 Correct 36 ms 30956 KB Output is correct
8 Correct 36 ms 30812 KB Output is correct
9 Correct 36 ms 30812 KB Output is correct
10 Correct 39 ms 30812 KB Output is correct
11 Correct 36 ms 30812 KB Output is correct
12 Correct 36 ms 30812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 30812 KB Output is correct
2 Correct 36 ms 30808 KB Output is correct
3 Correct 36 ms 30812 KB Output is correct
4 Correct 42 ms 30808 KB Output is correct
5 Correct 37 ms 30812 KB Output is correct
6 Correct 36 ms 30812 KB Output is correct
7 Correct 36 ms 30956 KB Output is correct
8 Correct 36 ms 30812 KB Output is correct
9 Correct 36 ms 30812 KB Output is correct
10 Correct 39 ms 30812 KB Output is correct
11 Correct 36 ms 30812 KB Output is correct
12 Correct 36 ms 30812 KB Output is correct
13 Correct 36 ms 30812 KB Output is correct
14 Correct 36 ms 30968 KB Output is correct
15 Correct 37 ms 30812 KB Output is correct
16 Correct 41 ms 30812 KB Output is correct
17 Correct 41 ms 30808 KB Output is correct
18 Correct 42 ms 30808 KB Output is correct
19 Correct 44 ms 30808 KB Output is correct
20 Correct 39 ms 30812 KB Output is correct
21 Correct 38 ms 30808 KB Output is correct
22 Correct 37 ms 30808 KB Output is correct
23 Correct 40 ms 30812 KB Output is correct
24 Correct 37 ms 30808 KB Output is correct
25 Correct 45 ms 30812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 30812 KB Output is correct
2 Correct 36 ms 30808 KB Output is correct
3 Correct 36 ms 30812 KB Output is correct
4 Correct 42 ms 30808 KB Output is correct
5 Correct 37 ms 30812 KB Output is correct
6 Correct 36 ms 30812 KB Output is correct
7 Correct 36 ms 30956 KB Output is correct
8 Correct 36 ms 30812 KB Output is correct
9 Correct 36 ms 30812 KB Output is correct
10 Correct 39 ms 30812 KB Output is correct
11 Correct 36 ms 30812 KB Output is correct
12 Correct 36 ms 30812 KB Output is correct
13 Correct 36 ms 30812 KB Output is correct
14 Correct 36 ms 30968 KB Output is correct
15 Correct 37 ms 30812 KB Output is correct
16 Correct 41 ms 30812 KB Output is correct
17 Correct 41 ms 30808 KB Output is correct
18 Correct 42 ms 30808 KB Output is correct
19 Correct 44 ms 30808 KB Output is correct
20 Correct 39 ms 30812 KB Output is correct
21 Correct 38 ms 30808 KB Output is correct
22 Correct 37 ms 30808 KB Output is correct
23 Correct 40 ms 30812 KB Output is correct
24 Correct 37 ms 30808 KB Output is correct
25 Correct 45 ms 30812 KB Output is correct
26 Correct 61 ms 31064 KB Output is correct
27 Correct 61 ms 31068 KB Output is correct
28 Correct 57 ms 31068 KB Output is correct
29 Correct 41 ms 30808 KB Output is correct
30 Correct 63 ms 31064 KB Output is correct
31 Correct 62 ms 31064 KB Output is correct
32 Correct 50 ms 30808 KB Output is correct
33 Correct 44 ms 30812 KB Output is correct
34 Correct 58 ms 31064 KB Output is correct
35 Correct 57 ms 31068 KB Output is correct
36 Correct 55 ms 31064 KB Output is correct
37 Correct 53 ms 31068 KB Output is correct
38 Correct 59 ms 31068 KB Output is correct
39 Correct 60 ms 31064 KB Output is correct
40 Correct 62 ms 31064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3743 ms 49856 KB Output is correct
2 Correct 3443 ms 49776 KB Output is correct
3 Correct 3128 ms 46756 KB Output is correct
4 Correct 3136 ms 49876 KB Output is correct
5 Correct 2794 ms 49840 KB Output is correct
6 Correct 153 ms 31964 KB Output is correct
7 Correct 2387 ms 56152 KB Output is correct
8 Correct 3192 ms 53584 KB Output is correct
9 Correct 2949 ms 55876 KB Output is correct
10 Correct 2978 ms 55868 KB Output is correct
11 Correct 3147 ms 55844 KB Output is correct
12 Correct 3080 ms 54504 KB Output is correct
13 Correct 3095 ms 56124 KB Output is correct
14 Correct 3240 ms 59108 KB Output is correct
15 Correct 3153 ms 56168 KB Output is correct
16 Correct 2950 ms 56156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 30812 KB Output is correct
2 Correct 36 ms 30808 KB Output is correct
3 Correct 36 ms 30812 KB Output is correct
4 Correct 42 ms 30808 KB Output is correct
5 Correct 37 ms 30812 KB Output is correct
6 Correct 36 ms 30812 KB Output is correct
7 Correct 36 ms 30956 KB Output is correct
8 Correct 36 ms 30812 KB Output is correct
9 Correct 36 ms 30812 KB Output is correct
10 Correct 39 ms 30812 KB Output is correct
11 Correct 36 ms 30812 KB Output is correct
12 Correct 36 ms 30812 KB Output is correct
13 Correct 36 ms 30812 KB Output is correct
14 Correct 36 ms 30968 KB Output is correct
15 Correct 37 ms 30812 KB Output is correct
16 Correct 41 ms 30812 KB Output is correct
17 Correct 41 ms 30808 KB Output is correct
18 Correct 42 ms 30808 KB Output is correct
19 Correct 44 ms 30808 KB Output is correct
20 Correct 39 ms 30812 KB Output is correct
21 Correct 38 ms 30808 KB Output is correct
22 Correct 37 ms 30808 KB Output is correct
23 Correct 40 ms 30812 KB Output is correct
24 Correct 37 ms 30808 KB Output is correct
25 Correct 45 ms 30812 KB Output is correct
26 Correct 61 ms 31064 KB Output is correct
27 Correct 61 ms 31068 KB Output is correct
28 Correct 57 ms 31068 KB Output is correct
29 Correct 41 ms 30808 KB Output is correct
30 Correct 63 ms 31064 KB Output is correct
31 Correct 62 ms 31064 KB Output is correct
32 Correct 50 ms 30808 KB Output is correct
33 Correct 44 ms 30812 KB Output is correct
34 Correct 58 ms 31064 KB Output is correct
35 Correct 57 ms 31068 KB Output is correct
36 Correct 55 ms 31064 KB Output is correct
37 Correct 53 ms 31068 KB Output is correct
38 Correct 59 ms 31068 KB Output is correct
39 Correct 60 ms 31064 KB Output is correct
40 Correct 62 ms 31064 KB Output is correct
41 Correct 3743 ms 49856 KB Output is correct
42 Correct 3443 ms 49776 KB Output is correct
43 Correct 3128 ms 46756 KB Output is correct
44 Correct 3136 ms 49876 KB Output is correct
45 Correct 2794 ms 49840 KB Output is correct
46 Correct 153 ms 31964 KB Output is correct
47 Correct 2387 ms 56152 KB Output is correct
48 Correct 3192 ms 53584 KB Output is correct
49 Correct 2949 ms 55876 KB Output is correct
50 Correct 2978 ms 55868 KB Output is correct
51 Correct 3147 ms 55844 KB Output is correct
52 Correct 3080 ms 54504 KB Output is correct
53 Correct 3095 ms 56124 KB Output is correct
54 Correct 3240 ms 59108 KB Output is correct
55 Correct 3153 ms 56168 KB Output is correct
56 Correct 2950 ms 56156 KB Output is correct
57 Correct 3883 ms 58808 KB Output is correct
58 Correct 3936 ms 56240 KB Output is correct
59 Correct 2671 ms 51648 KB Output is correct
60 Correct 3812 ms 62672 KB Output is correct
61 Correct 3878 ms 57868 KB Output is correct
62 Correct 2882 ms 54332 KB Output is correct
63 Correct 137 ms 31712 KB Output is correct
64 Correct 2916 ms 61968 KB Output is correct
65 Correct 3420 ms 56920 KB Output is correct
66 Correct 3715 ms 61620 KB Output is correct
67 Correct 2452 ms 61620 KB Output is correct
68 Correct 2644 ms 61320 KB Output is correct
69 Correct 2025 ms 51320 KB Output is correct
70 Correct 2991 ms 61516 KB Output is correct
71 Correct 3125 ms 61464 KB Output is correct
72 Correct 3330 ms 61624 KB Output is correct
73 Correct 3105 ms 61364 KB Output is correct
74 Correct 3997 ms 61400 KB Output is correct
75 Correct 2523 ms 51284 KB Output is correct
76 Correct 3705 ms 61452 KB Output is correct
77 Correct 3628 ms 57848 KB Output is correct