제출 #1336859

#제출 시각아이디문제언어결과실행 시간메모리
1336859LIACultivation (JOI17_cultivation)C++17
0 / 100
0 ms344 KiB
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define ll long long
#define v vector
#define lp(i, s, e) for (int i = s; i < e; ++i)

struct P {
  ll x, y;
  bool operator<(const P &o) const {
    if (x != o.x)
      return x < o.x;
    return y < o.y;
  }
};

ll R_grid, C_grid;
int N;
v<P> pts;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> R_grid >> C_grid >> N;

  pts.resize(N);
  lp(i, 0, N) { cin >> pts[i].x >> pts[i].y; }

  sort(pts.begin(), pts.end());

  v<ll> h_cands;
  lp(i, 0, N) {
    lp(j, i, N) { h_cands.push_back(pts[j].x - pts[i].x); }
  }

  sort(h_cands.begin(), h_cands.end());
  h_cands.erase(unique(h_cands.begin(), h_cands.end()), h_cands.end());

  ll ans = 4e18;

  lp(hi, 0, h_cands.size()) {
    ll H = h_cands[hi];
    ll max_gap = 0;

    lp(i, 1, N) { max_gap = max(max_gap, pts[i].x - pts[i - 1].x - 1); }
    if (max_gap > H)
      continue;

    ll L_req = 0, R_req = 0, W_req = 0;

    lp(i, 0, N) {
      ll min_y = 4e18, max_y = -4e18;
      bool found = false;

      lp(j, 0, N) {
        if (pts[j].x >= pts[i].x && pts[j].x <= pts[i].x + H) {
          min_y = min(min_y, pts[j].y);
          max_y = max(max_y, pts[j].y);
          found = true;
        }
      }

      if (found) {
        L_req = max(L_req, min_y - 1);
        R_req = max(R_req, C_grid - max_y);
      }
    }

    ll cur = max(L_req + R_req, W_req);
    ans = min(ans, H + cur);
  }

  cout << ans << "\n";
  return 0;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...