답안 #446679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446679 2021-07-23T06:21:55 Z baluteshih Cultivation (JOI17_cultivation) C++14
30 / 100
1972 ms 2516 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int INF = 2e9;
pii arr[305];

int umx, dmx;
vector<pii> smx; // [l, INF)

int lmi_a = INF;
vector<pii> lmi_u, lmi_d; // [0, r)
vector<pair<pii, int>> lmi_m; // [l, r)

int rmi_a = INF;
vector<pii> rmi_u, rmi_d; // [0, r)
vector<pair<pii, int>> rmi_m; // [l, r)

int smi_a = 0;
vector<pii> smi_u, smi_d; // [0, r)
vector<pair<pii, int>> smi_m; // [l, r)

vector<int> L, M, R;
vector<int> val;

void modify(vector<int> &seg, int ql, int qr, int l, int r, int rt, int v) {
    if (ql <= l && qr >= r)
        return seg[rt] = max(seg[rt], v), void();
    int mid = (l + r) >> 1;
    if (ql <= mid)
        modify(seg, ql, qr, l, mid, rt << 1, v);
    if (qr > mid)
        modify(seg, ql, qr, mid + 1, r, rt << 1 | 1, v);
}

void modify(vector<int> &seg, int ql, int qr, int v) { //[ql, qr)
    if (ql < qr)
        modify(seg, ql, qr - 1, 0, SZ(val) - 1, 1, v);
}

void dfs(vector<int> &seg, int l, int r, int rt, int v, vector<int> &res) {
    v = max(v, seg[rt]);
    if (l == r)
        return res[l] = v, void();
    int mid = (l + r) >> 1;
    dfs(seg, l, mid, rt << 1, v, res);
    dfs(seg, mid + 1, r, rt << 1 | 1, v, res);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int r, c, n, ans;
    cin >> r >> c >> n, ans = r + c - 2;
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i].X >> arr[i].Y;
        lmi_a = min(lmi_a, arr[i].Y - 1);
        rmi_a = min(rmi_a, c - arr[i].Y);
    }
    sort(arr + 1, arr + n + 1, [](pii a, pii b){ return a.Y < b.Y; });
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && arr[i].Y == arr[j].Y)
            ++j;
        if (j <= n)
            smi_a = max(smi_a, arr[j].Y - arr[i].Y - 1);
    }
    smi_a = max(smi_a, lmi_a + rmi_a);
    sort(arr + 1, arr + n + 1);
    // U
    for (int i = 1; i <= n; ++i) {
        int lft = -1, rgt = INF;
        for (int j = 1; arr[j].X < arr[i].X; ++j) {
            if (arr[j].Y >= arr[i].Y)
                rgt = min(rgt, arr[j].Y);
            if (arr[j].Y <= arr[i].Y)
                lft = max(lft, arr[j].Y);
        }
        if (lft == -1 && rgt == INF)
            umx = max(umx, arr[i].X - 1);
        else if (lft == -1)
            lmi_u.pb(pii(rgt - 1, arr[i].X - 1));
        else if (rgt == INF)
            rmi_u.pb(pii(c - lft, arr[i].X - 1));
        else if (rgt - lft - 1 > 0)
            smi_u.pb(pii(rgt - lft - 1, arr[i].X - 1));
    }
    sort(ALL(lmi_u)), sort(ALL(rmi_u)), sort(ALL(smi_u));
            
    // M
    for (int i = 1; i <= n; ++i)
        for (int j = 1; arr[j].X < arr[i].X; ++j) {
            int lft = -1, rgt = INF;
            for (int k = j + 1; k < i; ++k) {
                if (arr[k].Y >= arr[i].Y || arr[k].Y >= arr[j].Y)
                    rgt = min(rgt, arr[k].Y);
                if (arr[k].Y <= arr[i].Y || arr[k].Y <= arr[i].Y)
                    lft = max(lft, arr[k].Y);
            }
            if (lft == -1 && rgt == INF)
                smx.pb(pii(abs(arr[i].Y - arr[j].Y), arr[i].X - arr[j].X - 1));
            else if (lft == -1)
                lmi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), rgt - 1), arr[i].X - arr[j].X - 1));
            else if (rgt == INF)
                rmi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), c - lft), arr[i].X - arr[j].X - 1));
            else if (rgt - lft - 1 > abs(arr[i].Y - arr[i].Y))
                smi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), rgt - lft - 1), arr[i].X - arr[j].X - 1));
        }
    sort(ALL(lmi_m)), sort(ALL(rmi_m)), sort(ALL(smi_m)), sort(ALL(smx));

    // D
    for (int i = 1; i <= n; ++i) {
        int lft = -1, rgt = INF;
        for (int j = n; arr[j].X > arr[i].X; --j) {
            if (arr[j].Y >= arr[i].Y)
                rgt = min(rgt, arr[j].Y);
            if (arr[j].Y <= arr[i].Y)
                lft = max(lft, arr[j].Y);
        }
        if (lft == -1 && rgt == INF)
            dmx = max(dmx, r - arr[i].X);
        else if (lft == -1)
            lmi_d.pb(pii(rgt - 1, r - arr[i].X));
        else if (rgt == INF)
            rmi_d.pb(pii(c - lft, r - arr[i].X));
        else if (rgt - lft - 1 > 0)
            smi_d.pb(pii(rgt - lft - 1, r - arr[i].X));
    }
    sort(ALL(lmi_d)), sort(ALL(rmi_d)), sort(ALL(smi_d));

    L.pb(lmi_a);
    for (auto p : lmi_u)
        L.pb(p.X);
    for (auto p : lmi_m) {
        L.pb(p.X.X);
        L.pb(p.X.Y);
    }
    for (auto p : lmi_d)
        L.pb(p.X);
    sort(ALL(L)), L.resize(unique(ALL(L)) - L.begin());

    M.pb(smi_a);
    for (auto p : smi_u)
        M.pb(p.X);
    for (auto p : smi_m) {
        M.pb(p.X.X);
        M.pb(p.X.Y);
    }
    for (auto p : smi_d)
        M.pb(p.X);
    sort(ALL(M)), M.resize(unique(ALL(M)) - M.begin());

    R.pb(rmi_a);
    for (auto p : rmi_u)
        R.pb(p.X);
    for (auto p : rmi_m) {
        R.pb(p.X.X);
        R.pb(p.X.Y);
    }
    for (auto p : rmi_d)
        R.pb(p.X);
    sort(ALL(R)), R.resize(unique(ALL(R)) - R.begin());

    for (int lft : L) {
        if (lft < lmi_a)
            continue;
        val.clear();
        if (smi_a >= lft)
            val.pb(smi_a);
        val.pb(lft + rmi_a);
        for (int sum : M)
            if (sum >= lft)
                val.pb(sum);
        for (int rgt : R)
            val.pb(rgt + lft);
        sort(ALL(val)), val.resize(unique(ALL(val)) - val.begin());
        vector<int> seg_u(4 * SZ(val)), res_u(SZ(val));
        vector<int> seg_m(4 * SZ(val)), res_m(SZ(val));
        vector<int> seg_d(4 * SZ(val)), res_d(SZ(val));

        modify(seg_u, 0, SZ(val), umx);
        for (auto p : smx)
            modify(seg_m, lower_bound(ALL(val), p.X) - val.begin(), SZ(val), p.Y);
        modify(seg_d, 0, SZ(val), dmx);

        for (auto p : lmi_u)
            if (lft < p.X)
                modify(seg_u, 0, SZ(val), p.Y);
        for (auto p : lmi_m)
            if (lft < p.X.Y)
                modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), SZ(val), p.Y);
        for (auto p : lmi_d)
            if (lft < p.X)
                modify(seg_d, 0, SZ(val), p.Y);

        for (auto p : smi_u)
            modify(seg_u, 0, lower_bound(ALL(val), p.X) - val.begin(), p.Y);
        for (auto p : smi_m)
            modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), lower_bound(ALL(val), p.X.Y) - val.begin(), p.Y);
        for (auto p : smi_d)
            modify(seg_d, 0, lower_bound(ALL(val), p.X) - val.begin(), p.Y);

        for (auto p : rmi_u)
            modify(seg_u, 0, lower_bound(ALL(val), p.X + lft) - val.begin(), p.Y);
        for (auto p : rmi_m)
            modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), lower_bound(ALL(val), p.X.Y + lft) - val.begin(), p.Y);
        for (auto p : rmi_d)
            modify(seg_d, 0, lower_bound(ALL(val), p.X + lft) - val.begin(), p.Y);
        
        dfs(seg_u, 0, SZ(val) - 1, 1, 0, res_u);
        dfs(seg_m, 0, SZ(val) - 1, 1, 0, res_m);
        dfs(seg_d, 0, SZ(val) - 1, 1, 0, res_d);

        for (int i = 0; i < SZ(val); ++i) {
            if (val[i] < smi_a || val[i] - lft < rmi_a)
                continue;
            ans = min(ans, val[i] + max(res_m[i], res_u[i] + res_d[i]));
        }
    }

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 10 ms 332 KB Output is correct
25 Correct 5 ms 332 KB Output is correct
26 Correct 18 ms 460 KB Output is correct
27 Correct 22 ms 508 KB Output is correct
28 Correct 9 ms 440 KB Output is correct
29 Correct 20 ms 488 KB Output is correct
30 Correct 26 ms 464 KB Output is correct
31 Correct 20 ms 408 KB Output is correct
32 Correct 19 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 10 ms 332 KB Output is correct
25 Correct 5 ms 332 KB Output is correct
26 Correct 18 ms 460 KB Output is correct
27 Correct 22 ms 508 KB Output is correct
28 Correct 9 ms 440 KB Output is correct
29 Correct 20 ms 488 KB Output is correct
30 Correct 26 ms 464 KB Output is correct
31 Correct 20 ms 408 KB Output is correct
32 Correct 19 ms 588 KB Output is correct
33 Correct 490 ms 2148 KB Output is correct
34 Correct 7 ms 332 KB Output is correct
35 Correct 425 ms 964 KB Output is correct
36 Correct 1948 ms 2392 KB Output is correct
37 Correct 1655 ms 2268 KB Output is correct
38 Correct 1654 ms 2460 KB Output is correct
39 Correct 1757 ms 2076 KB Output is correct
40 Correct 1972 ms 2516 KB Output is correct
41 Correct 361 ms 940 KB Output is correct
42 Correct 171 ms 728 KB Output is correct
43 Correct 1488 ms 2260 KB Output is correct
44 Correct 411 ms 1064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 312 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Incorrect 2 ms 368 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 312 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Incorrect 2 ms 368 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 10 ms 332 KB Output is correct
25 Correct 5 ms 332 KB Output is correct
26 Correct 18 ms 460 KB Output is correct
27 Correct 22 ms 508 KB Output is correct
28 Correct 9 ms 440 KB Output is correct
29 Correct 20 ms 488 KB Output is correct
30 Correct 26 ms 464 KB Output is correct
31 Correct 20 ms 408 KB Output is correct
32 Correct 19 ms 588 KB Output is correct
33 Correct 490 ms 2148 KB Output is correct
34 Correct 7 ms 332 KB Output is correct
35 Correct 425 ms 964 KB Output is correct
36 Correct 1948 ms 2392 KB Output is correct
37 Correct 1655 ms 2268 KB Output is correct
38 Correct 1654 ms 2460 KB Output is correct
39 Correct 1757 ms 2076 KB Output is correct
40 Correct 1972 ms 2516 KB Output is correct
41 Correct 361 ms 940 KB Output is correct
42 Correct 171 ms 728 KB Output is correct
43 Correct 1488 ms 2260 KB Output is correct
44 Correct 411 ms 1064 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 2 ms 312 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 2 ms 332 KB Output is correct
50 Correct 2 ms 332 KB Output is correct
51 Incorrect 2 ms 368 KB Output isn't correct
52 Halted 0 ms 0 KB -