답안 #801986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
801986 2023-08-02T08:54:57 Z 이동현(#10092) Cultivation (JOI17_cultivation) C++17
30 / 100
2000 ms 600 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

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

    int r, c, n;
    cin >> r >> c >> n;
    assert(n <= 100);
    vector<array<int, 2>> a(n);
    for(int i = 0; i < n; ++i){
        cin >> a[i][0] >> a[i][1];
        --a[i][0];
        --a[i][1];
    }

    vector<int> dpu;
    sort(a.begin(), a.end());
    for(int i = 0; i < n; ++i){
        dpu.push_back(r - a[i][0]);
        dpu.push_back(r - a[i][0] - 1);
        for(int j = i + 1; j < n; ++j){
            dpu.push_back(a[j][0] - a[i][0]);
            dpu.push_back(a[j][0] - a[i][0] + 1);
        }
        for(int j = 0; j < n; ++j){
            dpu.push_back(a[j][0] + r - a[i][0]);
            dpu.push_back(a[j][0] + r - a[i][0] - 1);
        }
    }

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

    auto sol = [&](int dulen)->int{
        int rv = (int)1e18;

        vector<array<int, 3>> que;
        vector<vector<array<int, 3>>> mx(3);
        
        for(int j = 0; j < n; ++j){
            que.push_back({a[j][0], -1, a[j][1]});
            que.push_back({a[j][0] + dulen, 1, a[j][1]});
        }

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

        vector<int> st;

        for(int j = 0; j < (int)que.size(); ++j){
            auto i = que[j];
            if(!(int)st.size()){
                mx[0].push_back({i[0] - r + 1, -1, (int)1e18});
                mx[0].push_back({i[0], 1, (int)1e18});
            }
            if(i[1] == -1){
                st.push_back(i[2]);
            }
            else{
                st.erase(find(st.begin(), st.end(), i[2]));
            }

            if((j == (int)que.size() - 1 || que[j + 1][0] > i[0])){
                if(!(int)st.size()){
                    mx[0].push_back({i[0] - r + 1, -1, (int)1e18});
                    mx[0].push_back({i[0] + 1, 1, (int)1e18});
                }
                else{
                    sort(st.begin(), st.end());
                    mx[1].push_back({i[0] - r + 1, -1, *st.begin()});
                    mx[1].push_back({i[0] + 1, 1, *st.begin()});
                    mx[2].push_back({i[0] - r + 1, -1, c - *(--st.end()) - 1});
                    mx[2].push_back({i[0] + 1, 1, c - *(--st.end()) - 1});

                    int lst = (int)1e18, nmx = -1;
                    for(auto&p:st){
                        nmx = max(nmx, p - lst);
                        lst = p;
                    }
                    if(nmx != -1){
                        mx[0].push_back({i[0] - r + 1, -1, nmx});
                        mx[0].push_back({i[0] + 1, 1, nmx});
                    }
                }
            }
        }

        for(int i = 0; i < 3; ++i) sort(mx[i].begin(), mx[i].end());
        vector<multiset<int>> mxst(3);
        vector<int> pos(3);
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < 3; ++j){
                while(pos[j] < (int)mx[j].size() && mx[j][pos[j]][0] <= a[i][0]){
                    if(mx[j][pos[j]][1] == -1) mxst[j].insert(mx[j][pos[j]][2]);
                    else mxst[j].erase(mxst[j].find(mx[j][pos[j]][2]));
                    ++pos[j];
                }

                assert((int)mxst[j].size());
            }

            rv = min(rv, max(*(--mxst[0].end()) - 1, *(--mxst[1].end()) + *(--mxst[2].end())));
        }

        return rv;
    };

    int ans = (int)1e18;

    for(auto&dulen:dpu){
        if(dulen > 0) ans = min(ans, dulen - 1 + sol(dulen));
    }

    cout << ans << '\n';
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 344 KB Output is correct
2 Correct 37 ms 340 KB Output is correct
3 Correct 39 ms 340 KB Output is correct
4 Correct 42 ms 340 KB Output is correct
5 Correct 42 ms 340 KB Output is correct
6 Correct 10 ms 356 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 41 ms 340 KB Output is correct
9 Correct 18 ms 340 KB Output is correct
10 Correct 41 ms 340 KB Output is correct
11 Correct 47 ms 340 KB Output is correct
12 Correct 42 ms 340 KB Output is correct
13 Correct 6 ms 340 KB Output is correct
14 Correct 16 ms 356 KB Output is correct
15 Correct 8 ms 340 KB Output is correct
16 Correct 42 ms 340 KB Output is correct
17 Correct 53 ms 340 KB Output is correct
18 Correct 13 ms 356 KB Output is correct
19 Correct 46 ms 352 KB Output is correct
20 Correct 41 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 344 KB Output is correct
2 Correct 37 ms 340 KB Output is correct
3 Correct 39 ms 340 KB Output is correct
4 Correct 42 ms 340 KB Output is correct
5 Correct 42 ms 340 KB Output is correct
6 Correct 10 ms 356 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 41 ms 340 KB Output is correct
9 Correct 18 ms 340 KB Output is correct
10 Correct 41 ms 340 KB Output is correct
11 Correct 47 ms 340 KB Output is correct
12 Correct 42 ms 340 KB Output is correct
13 Correct 6 ms 340 KB Output is correct
14 Correct 16 ms 356 KB Output is correct
15 Correct 8 ms 340 KB Output is correct
16 Correct 42 ms 340 KB Output is correct
17 Correct 53 ms 340 KB Output is correct
18 Correct 13 ms 356 KB Output is correct
19 Correct 46 ms 352 KB Output is correct
20 Correct 41 ms 340 KB Output is correct
21 Execution timed out 2079 ms 600 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -