답안 #519710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519710 2022-01-27T05:29:32 Z xiaowuc1 Cultivation (JOI17_cultivation) C++17
0 / 100
1 ms 312 KB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;

void initcolumnsweep(vector<vector<int>>& columnsweep, vector<vector<int>>& lneed, vector<vector<int>>& rneed, const vector<pii>& v, int r, int c) {
  int n = sz(v);
  for(int i = 0; i < n; i++) {
    set<int> dp;
    dp.insert(-1);
    dp.insert(c);
    columnsweep[i].resize(n);
    lneed[i].resize(n);
    rneed[i].resize(n);
    fill(all(columnsweep[i]), c-1);
    fill(all(lneed[i]), c-1);
    fill(all(rneed[i]), c-1);
    multiset<int> intervals;
    intervals.insert(c);
    for(int j = i; j < n; j++) {
      int upneed = 1e9, downneed = 1e9;
      if(!dp.count(v[j].s)) {
        int lhs, rhs;
        {
          auto it = dp.ub(v[j].s);
          rhs = *(it--);
          lhs = *it;
        }
        dp.insert(v[j].s);
        int amt = rhs - lhs - 1;
        intervals.erase(intervals.find(amt));
        intervals.insert(v[j].s - lhs - 1);
        intervals.insert(rhs - v[j].s - 1);
      }
      upneed = *dp.ub(-1);
      {
        auto it = dp.rbegin(); it++;
        downneed = c-1 - *it;
      }
      columnsweep[i][j] = max(upneed + downneed, *intervals.rbegin());
      lneed[i][j] = upneed;
      rneed[i][j] = downneed;
    }
  }
}

void solve() {
  int r, c;
  cin >> r >> c;
  int n;
  cin >> n;
  vector<pii> v(n); // sorted in row major order
  for(auto& x: v) {
    cin >> x.f >> x.s;
    x.f--; x.s--;
  }
  int maxdist = 0;
  for(int i = 1; i < n; i++) updmax(maxdist, v[i].f - v[i-1].f - 1);
  sort(all(v));
  vector<vector<int>> columnsweep(n); // columnsweep[i][j] is the minimum number of cols needed to swap left/right
  vector<vector<int>> lhssweep(n); // lhssweep[i][j] is the minimum number of cols needed to go left
  vector<vector<int>> rhssweep(n); // rhssweep[i][j] is the minimum number of cols needed to go right
  initcolumnsweep(columnsweep, lhssweep, rhssweep, v, r, c);
  ll ret = 2e9;
  set<int> tried;
  auto compute = [&](int rowmove) {
    rowmove = max(rowmove, v[0].f);
    rowmove = max(rowmove, r-1-v[n-1].f);
    rowmove = max(rowmove, maxdist);
    if(tried.count(rowmove)) return;
    tried.insert(rowmove);
    int rhs = 0;
    ll upamt = 0;
    ll downamt = 0;
    ll maxamt = 0;
    for(int a = 0; a < n; a++) {
      while(rhs < n && v[rhs].f - v[a].f <= rowmove) {
        rhs++;
      }
      rhs--;
      updmax(maxamt, (ll)columnsweep[a][rhs]);
      updmax(upamt, (ll)lhssweep[a][rhs]);
      updmax(downamt, (ll)rhssweep[a][rhs]);
      if(rhs == n-1) break;
    }
    cerr << rowmove << ": " << maxamt << " " << upamt << " " << downamt << endl;
    updmin(ret, rowmove + max(maxamt, upamt+downamt));
  };
  for(int upidx = 0; upidx < n; upidx++) {
    compute(v[upidx].f);
    compute(r-1-v[upidx].f);
    for(int downidx = upidx; downidx < n; downidx++) {
      compute(v[downidx].f - v[upidx].f);
      compute(v[downidx].f - v[upidx].f+1);
      compute(v[downidx].f - v[upidx].f-1);
    }
  }
  cout << ret << "\n";
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -