QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431220#8645. Growing Vegetables is Fun 5nhuang6850 1ms3532kbC++206.2kb2024-06-05 06:27:502024-06-05 06:27:50

Judging History

你现在查看的是最新测评结果

  • [2024-06-05 06:27:50]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3532kb
  • [2024-06-05 06:27:50]
  • 提交

answer

/**
 * @author n685
 * @brief
 *
 *
 */
#include "bits/stdc++.h"

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif

struct II {
  std::vector<std::pair<int, int>> iv;
  II() {}
  II(std::vector<std::pair<int, int>> val) {
    for (auto p : val) {
      if (p.first <= p.second) {
        iv.push_back(p);
      }
    }
  }
  void compress() {
    // std::sort(iv.begin(), iv.end());
    if (iv.empty()) {
      return;
    }
    std::vector<std::pair<int, int>> niv{iv[0]};
    for (int i = 1; i < (int)iv.size(); ++i) {
      if (niv.back().second >= iv[i].first - 1) {
        niv.back().second = iv[i].second;
      } else {
        niv.push_back(iv[i]);
      }
    }
    iv = niv;
  }
};

std::pair<int, int> merge(std::pair<int, int> a, std::pair<int, int> b) {
  if (a.first > b.first) {
    std::swap(a, b);
  }
  assert(b.first <= a.second);
  if (a.first <= b.first && b.second <= a.second) {
    return b;
  }
  return std::pair{b.first, a.second};
}
II merge(II a, II b) {
  if (a.iv.empty() || b.iv.empty()) {
    return II();
  }
  II c;
  int j = 0;
  for (int i = 0; i < (int)a.iv.size(); ++i) {
    for (; j < (int)b.iv.size() && a.iv[i].second >= b.iv[j].first; ++j) {
      if (b.iv[j].second < a.iv[i].first) {
        if (j == (int)b.iv.size() - 1) {
          return c;
        }
        continue;
      }
      c.iv.push_back(merge(a.iv[i], b.iv[j]));
    }
  }
  c.compress();
  return c;
}
II join(II a, II b) {
  if (a.iv.empty()) {
    return b;
  } else if (b.iv.empty()) {
    return a;
  }
  if (a.iv[0].first > b.iv[0].first) {
    std::swap(a.iv, b.iv);
  }
  int j = 0;
  II c;
  for (int i = 0; i < (int)a.iv.size(); ++i) {
    c.iv.push_back(a.iv[i]);
    for (; j < (int)b.iv.size() &&
           (i == (int)a.iv.size() - 1 || a.iv[i + 1].first > b.iv[j].first);
         ++j) {
      c.iv.push_back(b.iv[j]);
    }
  }
  c.compress();
  return c;
}

int64_t solve(int n, std::vector<int64_t> a, std::vector<int64_t> b,
              std::vector<int64_t> c) {
  auto good = [&](int64_t k) -> bool {
    dbg(k);
    std::vector<II> ii(2 * n); // when each of them work
    int r = 2 * n - 1;
    int lb = 0, rb = 0;
    int lb2 = 0, rb2 = 0;
    for (int i = 0; i < n; ++i) {
      // l <= i
      while (lb < n && b[lb] < a[i] - k) {
        ++lb;
      }
      ++rb;
      while (rb < n && b[rb] <= a[i] + k) {
        ++rb;
      }
      --rb;
      while (a[r] < a[i]) {
        --r;
      }
      II f, s;
      if (lb <= rb && rb < n && std::abs(b[lb] - a[i]) <= k &&
          std::abs(b[rb] - a[i]) <= k) {
        f.iv.emplace_back(0, i);
        // from 0 <= l <= r - n + 1: i - l must be in [lb, rb]
        // afterwards from r - n + 2 <= l <= i: i - r + n - 1 must be in
        // [lb, rb]
        II m;
        if (lb <= i - r + n - 1 && i - r + n - 1 <= rb) {
          m = II({{i - rb, i - lb}, {r - n + 2, i}});
        } else {
          m = II({{i - rb, i - lb}});
        }
        m.compress();
        f = merge(f, m);
      }

      // i + 1 <= l <= n
      while (lb2 < n && c[lb2] < a[i] - k) {
        ++lb2;
      }
      ++rb2;
      while (rb2 < n && c[rb2] <= a[i] + k) {
        ++rb2;
      }
      --rb2;
      if (lb2 <= rb2 && rb2 < n && std::abs(c[lb2] - a[i]) <= k &&
          std::abs(c[rb2] - a[i]) <= k) {
        s.iv.emplace_back(i + 1, n);
        II m;
        if (lb2 <= 2 * n - 2 - r + i && 2 * n - 2 - r + i <= rb2) {
          m = II({{(n - 1) + i - rb2, (n - 1) + i - lb2}, {r - n + 1, n}});
        } else {
          m = II({{(n - 1) + i - rb2, (n - 1) + i - lb2}});
        }
        m.compress();
        s = merge(s, m);
      }
      ii[i] = join(f, s);
    }
    r = 0;
    lb = 0, rb = 0;
    lb2 = 0, rb2 = 0;
    for (int i = 2 * n - 1; i >= n; --i) {
      while (lb < n && c[lb] < a[i] - k) {
        ++lb;
      }
      ++rb;
      while (rb < n && c[rb] <= a[i] + k) {
        ++rb;
      }
      --rb;
      if (a[r] < a[i]) {
        ++r;
      }
      II f, s;
      // 0 <= l <= i - n
      if (lb <= rb && rb < n && std::abs(c[lb] - a[i]) <= k &&
          std::abs(c[rb] - a[i]) <= k) {
        f.iv.emplace_back(0, i - n);
        II m;
        if (i - n > r && lb <= (2 * n - 1) - i + r &&
            (2 * n - 1) - i + r <= rb) {
          m = II(
              {{lb + i - (2 * n - 1), rb + i - (2 * n - 1)}, {r + 1, i - n}});
        } else {
          m = II({{lb + i - (2 * n - 1), rb + i - (2 * n - 1)}});
        }
        m.compress();
        f = merge(f, m);
      }
      // i - n + 1 <= l <= n
      while (lb2 < n && b[lb2] < a[i] - k) {
        ++lb2;
      }
      ++rb2;
      while (rb2 < n && b[rb2] <= a[i] + k) {
        ++rb2;
      }
      --rb2;
      if (lb2 <= rb2 && rb2 < n && std::abs(b[lb2] - a[i]) <= k &&
          std::abs(b[rb2] - a[i]) <= k) {
        s.iv.emplace_back(i - n + 1, n);
        II m;
        if (i - n + 1 <= r && lb2 <= r - i + n - 1 && r - i + n - 1 <= rb2) {
          m = II({{i - n + 1, r}, {lb2 + i - n, rb2 + i - n}});
        } else {
          m = II({{lb2 + i - n, rb2 + i - n}});
        }
        m.compress();
        s = merge(s, m);
      }
      ii[i] = join(f, s);
    }
    II ans({{0, n}});
    for (int i = 0; i < 2 * n; ++i) {
      ans = merge(ans, ii[i]);
      dbg(i, ii[i].iv, ans.iv);
      if (ans.iv.empty()) {
        return false;
      }
    }
    nline();
    return true;
  };
  int64_t l = 0, r = (int64_t)1e9;
  while (l < r) {
    int64_t mid = (l + r) / 2;
    if (good(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  return l;
}

int main() {
#ifndef LOCAL
  std::cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int n;
  std::cin >> n;
  std::vector<int64_t> a(2 * n), b(n), c(n);
  for (int i = 0; i < 2 * n; ++i) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    std::cin >> b[i];
  }
  for (int i = 0; i < n; ++i) {
    std::cin >> c[i];
  }
  std::sort(b.begin(), b.end());
  std::sort(c.begin(), c.end());

  std::cout << std::min(solve(n, a, b, c), solve(n, a, c, b)) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3532kb

input:

5
71039844 102827355 217531548 220229557 237972610 314010999 309303025 282414480 227324750 127910276
355992665 573821998 974848115 193721993 786455947
602158296 281786121 321727541 140763116 455641294

output:

384601450

result:

wrong answer 1st lines differ - expected: '660837116', found: '384601450'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #41:

score: 0
Time Limit Exceeded

input:

300000
619 2319 3493 3715 4353 4926 8022 14367 18919 23666 25565 25582 27245 27303 28880 29568 29918 31815 34986 36861 40022 40147 47041 52120 55624 57816 58553 59319 60056 63512 66442 68907 68915 69471 69714 71129 76758 76761 77220 78196 84558 87950 88697 88782 89810 90113 92091 95040 97273 99148 1...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #1:

0%