답안 #1074454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1074454 2024-08-25T10:35:37 Z joelgun14 Growing Vegetables is Fun 5 (JOI24_vegetables5) C++17
67 / 100
5000 ms 43448 KB
// header file
#include <bits/stdc++.h>
// pragma
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC opitmize("Ofast")
#pragma GCC opitmize("unroll-loops")
// 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;
struct fenwick {
  int a[lim];
  fenwick() {
    memset(a, 0, sizeof(a));
  }
  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;
    update(l, val);
    update(r + 1, -val);
  }
  int query(int idx) {
    if(idx >= lim)
      idx = lim - 1;
    int res = 0;
    while(idx) {
      res += a[idx];
      idx -= idx&-idx;
    }
    return res;
  }
} cur;
struct disjoint_setr {
  int h[lim];
  void reset() {
    memset(h, -1, sizeof(h));
  }
  disjoint_setr() {
    reset();
  }
  int nxt(int x) {
    return h[x] == -1 ? x : h[x] = nxt(h[x]);
  }
  void erase(int x) {
    merge(x, x + 1);
  }
  void merge(int x, int y) {
    x = nxt(x), y = nxt(y);
    if(x != y) {
      if(x < y)
        swap(x, y);
      h[y] = x;
    }
  }
} redr, bluer;
struct disjoint_setl {
  int h[lim];
  void reset() {
    memset(h, -1, sizeof(h));
  }
  disjoint_setl() {
    reset();
  }
  int prv(int x) {
    return h[x] == -1 ? x : h[x] = prv(h[x]);
  }
  void erase(int x) {
    merge(x - 1, x);
  }
  void merge(int x, int y) {
    x = prv(x), y = prv(y);
    if(x != y) {
      if(x > y)
        swap(x, y);
      h[y] = x;
    }
  }
} 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(cur.a, 0, sizeof(cur.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
    for(int i = 1; i <= 2 * n; ++i) {
      // idx of element >= a[i] - mid
      validb[i].fi = lower_bound(b + 1, b + n + 1, a[i] - mid) - b;
      // idx of element <= a[i] + mid
      validb[i].se = upper_bound(b + 1, b + n + 1, a[i] + mid) - b - 1;
      // idx of element >= a[i] - mid
      validr[i].fi = lower_bound(c + 1, c + n + 1, a[i] - mid) - c;
      // idx of element <= a[i] + mid
      validr[i].se = upper_bound(c + 1, c + n + 1, a[i] + mid) - c - 1;
      // if(mid == 1) {
      //   cerr << a[i] + mid << " " << upper_bound(c + 1, c + n + 1, a[i] + mid) - c - 1 << " " << validr[i].se << endl;
      // }
    }
    // cerr << "TEST" << endl;
    for(auto p : v) {
      // process
      // cerr << "UPDATE" << endl;
      cur.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 = cur.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 = cur.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) {
        cur.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 = cur.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 = cur.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 << " " << cur.query(*it) << " " << validr[idx].fi << endl; 
        int tmp2 = cur.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 << " " << cur.query(*it) << " " << validr[idx].se << endl; 
        int tmp2 = cur.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 << " " << cur.query(*it) << " " << validr[idx].fi << endl; 
          int tmp2 = cur.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 << " " << cur.query(*it) << " " << validr[idx].se << endl; 
          int tmp2 = cur.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;
}

Compilation message

Main.cpp:5: warning: ignoring '#pragma GCC opitmize' [-Wunknown-pragmas]
    5 | #pragma GCC opitmize("Ofast")
      | 
Main.cpp:6: warning: ignoring '#pragma GCC opitmize' [-Wunknown-pragmas]
    6 | #pragma GCC opitmize("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12124 KB Output is correct
2 Correct 9 ms 12192 KB Output is correct
3 Correct 9 ms 12124 KB Output is correct
4 Correct 9 ms 11956 KB Output is correct
5 Correct 9 ms 12124 KB Output is correct
6 Correct 9 ms 12196 KB Output is correct
7 Correct 8 ms 12064 KB Output is correct
8 Correct 9 ms 12124 KB Output is correct
9 Correct 9 ms 11956 KB Output is correct
10 Correct 10 ms 12120 KB Output is correct
11 Correct 10 ms 12192 KB Output is correct
12 Correct 8 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12124 KB Output is correct
2 Correct 9 ms 12192 KB Output is correct
3 Correct 9 ms 12124 KB Output is correct
4 Correct 9 ms 11956 KB Output is correct
5 Correct 9 ms 12124 KB Output is correct
6 Correct 9 ms 12196 KB Output is correct
7 Correct 8 ms 12064 KB Output is correct
8 Correct 9 ms 12124 KB Output is correct
9 Correct 9 ms 11956 KB Output is correct
10 Correct 10 ms 12120 KB Output is correct
11 Correct 10 ms 12192 KB Output is correct
12 Correct 8 ms 12124 KB Output is correct
13 Correct 9 ms 12124 KB Output is correct
14 Correct 12 ms 12196 KB Output is correct
15 Correct 10 ms 12120 KB Output is correct
16 Correct 9 ms 12124 KB Output is correct
17 Correct 11 ms 12120 KB Output is correct
18 Correct 11 ms 12124 KB Output is correct
19 Correct 8 ms 12004 KB Output is correct
20 Correct 9 ms 12196 KB Output is correct
21 Correct 10 ms 12196 KB Output is correct
22 Correct 9 ms 12124 KB Output is correct
23 Correct 10 ms 12200 KB Output is correct
24 Correct 12 ms 12120 KB Output is correct
25 Correct 11 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12124 KB Output is correct
2 Correct 9 ms 12192 KB Output is correct
3 Correct 9 ms 12124 KB Output is correct
4 Correct 9 ms 11956 KB Output is correct
5 Correct 9 ms 12124 KB Output is correct
6 Correct 9 ms 12196 KB Output is correct
7 Correct 8 ms 12064 KB Output is correct
8 Correct 9 ms 12124 KB Output is correct
9 Correct 9 ms 11956 KB Output is correct
10 Correct 10 ms 12120 KB Output is correct
11 Correct 10 ms 12192 KB Output is correct
12 Correct 8 ms 12124 KB Output is correct
13 Correct 9 ms 12124 KB Output is correct
14 Correct 12 ms 12196 KB Output is correct
15 Correct 10 ms 12120 KB Output is correct
16 Correct 9 ms 12124 KB Output is correct
17 Correct 11 ms 12120 KB Output is correct
18 Correct 11 ms 12124 KB Output is correct
19 Correct 8 ms 12004 KB Output is correct
20 Correct 9 ms 12196 KB Output is correct
21 Correct 10 ms 12196 KB Output is correct
22 Correct 9 ms 12124 KB Output is correct
23 Correct 10 ms 12200 KB Output is correct
24 Correct 12 ms 12120 KB Output is correct
25 Correct 11 ms 12124 KB Output is correct
26 Correct 41 ms 12376 KB Output is correct
27 Correct 43 ms 12376 KB Output is correct
28 Correct 32 ms 12124 KB Output is correct
29 Correct 10 ms 12120 KB Output is correct
30 Correct 41 ms 12380 KB Output is correct
31 Correct 36 ms 12124 KB Output is correct
32 Correct 24 ms 12124 KB Output is correct
33 Correct 15 ms 12124 KB Output is correct
34 Correct 30 ms 12384 KB Output is correct
35 Correct 32 ms 12124 KB Output is correct
36 Correct 39 ms 12376 KB Output is correct
37 Correct 31 ms 12124 KB Output is correct
38 Correct 31 ms 12376 KB Output is correct
39 Correct 32 ms 12380 KB Output is correct
40 Correct 33 ms 12376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4921 ms 34856 KB Output is correct
2 Correct 4547 ms 35772 KB Output is correct
3 Correct 3999 ms 31044 KB Output is correct
4 Correct 4156 ms 37192 KB Output is correct
5 Correct 4219 ms 36916 KB Output is correct
6 Correct 146 ms 13016 KB Output is correct
7 Correct 3548 ms 36428 KB Output is correct
8 Correct 3903 ms 31044 KB Output is correct
9 Correct 4112 ms 36020 KB Output is correct
10 Correct 4135 ms 36388 KB Output is correct
11 Correct 4254 ms 36832 KB Output is correct
12 Correct 4053 ms 36792 KB Output is correct
13 Correct 4224 ms 35912 KB Output is correct
14 Correct 4264 ms 36532 KB Output is correct
15 Correct 4075 ms 32748 KB Output is correct
16 Correct 3811 ms 36668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12124 KB Output is correct
2 Correct 9 ms 12192 KB Output is correct
3 Correct 9 ms 12124 KB Output is correct
4 Correct 9 ms 11956 KB Output is correct
5 Correct 9 ms 12124 KB Output is correct
6 Correct 9 ms 12196 KB Output is correct
7 Correct 8 ms 12064 KB Output is correct
8 Correct 9 ms 12124 KB Output is correct
9 Correct 9 ms 11956 KB Output is correct
10 Correct 10 ms 12120 KB Output is correct
11 Correct 10 ms 12192 KB Output is correct
12 Correct 8 ms 12124 KB Output is correct
13 Correct 9 ms 12124 KB Output is correct
14 Correct 12 ms 12196 KB Output is correct
15 Correct 10 ms 12120 KB Output is correct
16 Correct 9 ms 12124 KB Output is correct
17 Correct 11 ms 12120 KB Output is correct
18 Correct 11 ms 12124 KB Output is correct
19 Correct 8 ms 12004 KB Output is correct
20 Correct 9 ms 12196 KB Output is correct
21 Correct 10 ms 12196 KB Output is correct
22 Correct 9 ms 12124 KB Output is correct
23 Correct 10 ms 12200 KB Output is correct
24 Correct 12 ms 12120 KB Output is correct
25 Correct 11 ms 12124 KB Output is correct
26 Correct 41 ms 12376 KB Output is correct
27 Correct 43 ms 12376 KB Output is correct
28 Correct 32 ms 12124 KB Output is correct
29 Correct 10 ms 12120 KB Output is correct
30 Correct 41 ms 12380 KB Output is correct
31 Correct 36 ms 12124 KB Output is correct
32 Correct 24 ms 12124 KB Output is correct
33 Correct 15 ms 12124 KB Output is correct
34 Correct 30 ms 12384 KB Output is correct
35 Correct 32 ms 12124 KB Output is correct
36 Correct 39 ms 12376 KB Output is correct
37 Correct 31 ms 12124 KB Output is correct
38 Correct 31 ms 12376 KB Output is correct
39 Correct 32 ms 12380 KB Output is correct
40 Correct 33 ms 12376 KB Output is correct
41 Correct 4921 ms 34856 KB Output is correct
42 Correct 4547 ms 35772 KB Output is correct
43 Correct 3999 ms 31044 KB Output is correct
44 Correct 4156 ms 37192 KB Output is correct
45 Correct 4219 ms 36916 KB Output is correct
46 Correct 146 ms 13016 KB Output is correct
47 Correct 3548 ms 36428 KB Output is correct
48 Correct 3903 ms 31044 KB Output is correct
49 Correct 4112 ms 36020 KB Output is correct
50 Correct 4135 ms 36388 KB Output is correct
51 Correct 4254 ms 36832 KB Output is correct
52 Correct 4053 ms 36792 KB Output is correct
53 Correct 4224 ms 35912 KB Output is correct
54 Correct 4264 ms 36532 KB Output is correct
55 Correct 4075 ms 32748 KB Output is correct
56 Correct 3811 ms 36668 KB Output is correct
57 Correct 4881 ms 36140 KB Output is correct
58 Execution timed out 5037 ms 43448 KB Time limit exceeded
59 Halted 0 ms 0 KB -