QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#557036#360. Cultivationmakrav0 0ms3788kbC++209.4kb2024-09-11 00:27:462024-09-11 00:27:47

Judging History

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

  • [2024-09-11 00:27:47]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:3788kb
  • [2024-09-11 00:27:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
#define int ll

const int INF = 4e9;

void solve() {
    int h, w, n; cin >> h >> w >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++) cin >> a[i].ff >> a[i].sc;
    vector<vector<bool>> gp(n, vector<bool>(n, false));
    sort(all(a), [](pair<int, int> x, pair<int, int> y) {
        return x.second < y.second;
    });
    for (int i = 0; i < n; i++) {
        vector<vector<vector<int>>> pts(2, vector<vector<int>>(2));
        for (int j = 0; j < n; j++) {
            if (j == i) continue;
            pts[(a[j].second >= a[i].second)][(a[j].first >= a[i].first)].pb(j);
        }
        for (int j = 0; j < 2; j++) reverse(all(pts[0][j]));
        for (int i1 = 0; i1 < 2; i1++) {
            for (int j = 0; j < 2; j++) {
                int mind = INF;
                for (int k = 0; k < pts[i1][j].size(); k++) {
                    if (abs(a[pts[i1][j][k]].first - a[i].first) < mind) {
                        gp[i][pts[i1][j][k]] = true;
                        mind = abs(a[pts[i1][j][k]].first - a[i].first);
                    }
                }
            }
        }
    }
    vector<pair<int, int>> gps;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (gp[i][j]) {
                //cout << i << ' ' << j << '\n';
                gps.pb({i, j});
            }
        }
    }
    vector<ll> delta(sz(gps));
    for (int i = 0; i < sz(gps); i++) {
        ll max_up = 0, min_d = h + 1;
        int mxy = max(a[gps[i].first].first, a[gps[i].second].first), mny = min(a[gps[i].first].first, a[gps[i].second].first);
        for (int j = 0; j < n; j++) {
            if (a[gps[i].first].second < a[j].second && a[j].second < a[gps[i].second].second) {
                if (a[j].first > mxy) min_d = min(min_d, (ll)a[j].first);
                else {
                    assert(a[j].first < mny);
                    max_up = max(max_up, (ll)a[j].first);
                }
            }
        }
        delta[i] = (min_d - max_up - 1);
    }
    vector<int> never_left(n), never_right(n);
    vector<ll> lol_left(n), lol_right(n);
    for (int i = 0; i < n; i++) {
        {
            // left
            int max_up = 0, min_d = h + 1;
            for (int j = 0; j < n; j++) {
                if (a[j].second < a[i].second) {
                    if (a[j].first == a[i].first) {
                        never_left[i] = 1;
                        break;
                    }
                    if (a[j].first < a[i].first) max_up = max(max_up, a[j].first);
                    else min_d = min(min_d, a[j].first);
                }
            }
            lol_left[i] = min_d - max_up;
        } 
        {
            // right
            int max_up = 0, min_d = h + 1;
            for (int j = 0; j < n; j++) {
                if (a[j].second > a[i].second) {
                    if (a[j].first == a[i].first) {
                        never_right[i] = 1;
                        break;
                    }
                    if (a[j].first < a[i].first) max_up = max(max_up, a[j].first);
                    else min_d = min(min_d, a[j].first);
                }
            }
            lol_right[i] = min_d - max_up;
        }
    }   

    vector<int> ord_left(n), ord_right(n);
    iota(all(ord_left), 0); iota(all(ord_right), 0);
    sort(all(ord_left), [&](int i, int j) {
        return a[i].second < a[j].second;
    });
    sort(all(ord_right), [&](int i, int j) {
        return a[i].second > a[j].second;
    });
    vector<int> pos_left(n), pos_right(n);
    for (int i = 0; i < n; i++) {
        pos_left[ord_left[i]] = i;
        pos_right[ord_right[i]] = i;
    }

    vector<int> ups;
    for (int i = 0; i < n; i++) ups.pb(a[i].first - 1);

    vector<int> alds;
    for (int i = 0; i < sz(gps); i++) {
        alds.pb(a[gps[i].second].second - a[gps[i].first].second - 1);
    }
    sort(all(alds));
    alds.resize(unique(all(alds)) - alds.begin());
    map<int, int> pos;
    for (int i = 0; i < sz(alds); i++) pos[alds[i]] = i;
    vector<int> ind_mid(sz(gps));
    for (int i = 0; i < sz(gps); i++) ind_mid[i] = pos[a[gps[i].second].second - a[gps[i].first].second - 1];

    vector<int> ordp1(sz(gps)) /* order by delta of basic y values */, ordp2(sz(gps)) /* order by delta of second y values */;
    vector<int> vals(sz(gps)), W(sz(gps));
    for (int i = 0; i < sz(gps); i++) {
        vals[i] = abs(a[gps[i].first].first - a[gps[i].second].first);
        W[i] = a[gps[i].second].second - a[gps[i].first].second;
    }
    iota(all(ordp1), 0);
    sort(all(ordp1), [&](int i, int j){
        return vals[i] < vals[j];
    });
    iota(all(ordp2), 0);
    sort(all(ordp2), [&](int i, int j) {
        return delta[i] < delta[j];
    });

    int need_down = INF;
    for (int i = 0; i < n; i++) need_down = min(need_down, h - a[i].first);
    // delta already with -1
    auto solve = [&](int up) -> long long {
        // returns minimal answer with current up value
        vector<pair<int, int>> lefts, rights;
        vector<int> used_left(n, 0), used_right(n, 0), cnt_mid(sz(alds));
        vector<pair<int, int>> start_mid, endd_mid;
        for (int i = 0; i < n; i++) {
            {
                if (never_left[i]) continue;
                if (lol_left[i] - 1 <= up) {
                    continue;
                }
                used_left[pos_left[i]] = 1;
                lefts.pb({lol_left[i] - 1 - up, pos_left[i]});
                //cout << i << ' ' << up << ' ' << lol_left[i] - 1 - up << '\n';
            }
            {
                if (never_right[i]) continue;
                if (lol_right[i] - 1 <= up) {
                    continue;
                }
                used_right[pos_right[i]] = 1;
                rights.pb({lol_right[i] - 1 - up, pos_right[i]});
            }
        }
        sort(all(lefts)); sort(all(rights));
        int pmax_left = -1, pmax_right = -1, pmid = -1;
        auto update_left = [&]() {
            while (pmax_left >= 0 && !used_left[pmax_left]) pmax_left--;
        };
        auto update_right = [&]() {
            while (pmax_right >= 0 && !used_right[pmax_right]) pmax_right--;
        };
        auto update_mid = [&]() {
            while (pmid >= 0 && !cnt_mid[pmid]) pmid--;
        };
        for (int i = 0; i < n; i++) {
            if (used_left[i]) pmax_left = i;
            if (used_right[i]) pmax_right = i;
        }

        for (int i = 0; i < sz(gps); i++) {
            start_mid.pb({ind_mid[ordp1[i]], max(0ll, vals[ordp1[i]] - up)});
        }
        for (int i = 0; i < sz(gps); i++) {
            if (delta[ordp2[i]] > up) endd_mid.pb({ind_mid[ordp1[i]], delta[ordp2[i]] - up});
        }
        int ind_start = 0, ind_endd = 0, ind_left = 0, ind_right = 0;
        while (ind_start < sz(start_mid) && start_mid[ind_start].second == 0) {
            cnt_mid[start_mid[ind_start].first]++;
            pmid = max(pmid, start_mid[ind_start].first);
            ind_start++;
        }
        int A = 4 * INF;
        while (ind_start < sz(start_mid) || ind_left < sz(lefts) || ind_right < sz(rights) || ind_endd < sz(endd_mid)) {
            int mnt = 4 * INF;
            if (ind_start < sz(start_mid)) mnt = min(mnt, start_mid[ind_start].second);
            if (ind_left < sz(lefts)) mnt = min(mnt, lefts[ind_left].first);
            if (ind_right < sz(rights)) mnt = min(mnt, rights[ind_right].first);
            if (ind_endd < sz(endd_mid)) mnt = min(mnt, endd_mid[ind_endd].second);

            while (ind_start < sz(start_mid) && start_mid[ind_start].second == mnt) {
                cnt_mid[start_mid[ind_start].first]++;
                ind_start++;
            }
            while (ind_endd < sz(endd_mid) && endd_mid[ind_endd].second == mnt) {
                cnt_mid[endd_mid[ind_endd].first]--;
                ind_endd++;
            }
            while (ind_left < sz(lefts) && lefts[ind_left].first == mnt) {
                used_left[lefts[ind_left].second] = 0;
                ind_left++;
            }
            while (ind_right < sz(rights) && rights[ind_right].first == mnt) {
                used_right[rights[ind_right].second] = 0;
                ind_right++;
            }

            update_left(); update_mid(); update_right();
            int cl = (pmax_left == -1 ? 0 : a[ord_left[pmax_left]].second - 1), cr = (pmax_right == -1 ? 0 : w - a[ord_right[pmax_right]].second), 
                     cmid = (pmid == -1 ? 0 : alds[pmid]);
            //cout << up << ' ' << mnt << ' ' << cl << ' ' << cr << ' ' << cmid << '\n';
            if (mnt >= need_down) {
                A = min(A, mnt + max(cl + cr, cmid));
            }

        }
        return A + up;
    };  

    ll ans = 4000000000;
    for (int up : ups) {
        ans = min(ans, solve(up));
    }
    cout << ans << '\n';
}

signed main() {
    int tt = 1;
    #ifdef LOCAL 
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        cin >> tt;
    #else
        ios::sync_with_stdio(false); 
        cin.tie(0); cout.tie(0);
    #endif

    while (tt--) {
        solve();
    }

    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3572kb

input:

2 4
2
1 1
1 4

output:

2

result:

wrong answer 1st lines differ - expected: '3', found: '2'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #45:

score: 0
Wrong Answer
time: 0ms
memory: 3788kb

input:

1000000000 1000000000
17
822413671 70423910
260075513 431043546
300945721 793553248
142848049 163787897
392462410 831950868
699005697 111397300
444396260 130450496
642691616 595456084
467968916 463598810
159764248 611476406
929313754 539645102
365153650 964108073
906780716 373514044
970118116 655138...

output:

968616999

result:

wrong answer 1st lines differ - expected: '852626202', found: '968616999'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%