답안 #447704

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447704 2021-07-27T11:18:40 Z Haruto810198 Cultivation (JOI17_cultivation) C++17
60 / 100
2000 ms 972 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double

#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))
#define szof(x) ((int)(x).size())

#define vi vector<int>
#define pii pair<int,int>

#define F first
#define S second

#define pb push_back
#define eb emplace_back
#define mkp make_pair

const int INF = 2147483647;
const int LNF = 1e18;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

const int MAX = 310;

int n;
int R, C;
pii pt[MAX];

int res;

int check(int dxr){

    //cerr<<"check("<<dxr<<") : "<<endl;

    multiset<int> st;
    vector<vi> ev;
    /// insert : time,  1, pos
    /// erase  : time, -1, pos

    vector<vi> seg; /// Lpos, Rpos, minSum, [L, R]

    FOR(i, 1, n, 1){
        ev.pb({pt[i].F, 1, pt[i].S});
        ev.pb({pt[i].F + dxr + 1, -1, pt[i].S});
    }

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

    int pv_time = -LNF;
    int Ldis = LNF, Rdis = LNF;
    multiset<int> sums;

    for(auto E : ev){

        int Time = E[0];
        int type = E[1];
        int pos = E[2];

        if(pv_time < Time){
            if(!sums.empty()){
                seg.pb({Ldis, Rdis, *prev(sums.end()), pv_time, Time - 1});
            }
            else{
                seg.pb({Ldis, Rdis, 0, pv_time, Time - 1});
            }
        }

        pv_time = Time;

        if(type == 1){

            st.insert(pos);

            auto it = st.find(pos);
            auto pit = prev(it);
            auto nit = next(it);

            if(it != st.begin() and it != prev(st.end()) ){
                sums.erase( sums.find(*nit - *pit - 1) );
                sums.insert(*nit - *it - 1);
                sums.insert(*it - *pit - 1);
            }
            else if(it == st.begin() and it != prev(st.end()) ){
                sums.insert(*nit - *it - 1);
            }
            else if(it != st.begin() and it == prev(st.end()) ){
                sums.insert(*it - *pit - 1);
            }

        }
        else{

            auto it = st.find(pos);
            auto pit = prev(it);
            auto nit = next(it);

            if(it != st.begin() and it != prev(st.end()) ){
                sums.insert(*nit - *pit - 1);
                sums.erase( sums.find(*nit - *it - 1) );
                sums.erase( sums.find(*it - *pit - 1) );
            }
            else if(it == st.begin() and it != prev(st.end()) ){
                sums.erase( sums.find(*nit - *it - 1) );
            }
            else if(it != st.begin() and it == prev(st.end()) ){
                sums.erase( sums.find(*it - *pit - 1) );
            }

            st.erase(it);

        }

        if(st.empty()){
            Ldis = LNF;
            Rdis = LNF;
        }
        else{
            Ldis = *st.begin() - 1;
            Rdis = C - *prev(st.end());
        }

        /*
        cerr<<"st : ";
        for(int i : st){
            cerr<<i<<" ";
        }
        cerr<<endl;

        cerr<<"sums : ";
        for(int i : sums){
            cerr<<i<<" ";
        }
        cerr<<endl;
        */

    }

    seg.pb({LNF, LNF, LNF, pv_time, LNF});

    /*
    cerr<<"seg : "<<endl;

    for(auto I : seg){

        cerr<<"{";
        for(auto i : I){
            cerr<<i<<", ";
        }
        cerr<<"}"<<endl;

    }
    */

    int ret = LNF;

    int lptr = 0, rptr = 0;
    multiset<int> Ls, Rs, Sums;

    Ls.insert(seg[0][0]);
    Rs.insert(seg[0][1]);
    Sums.insert(seg[0][2]);

    while(1){

        /// move rptr
        while( (seg[rptr][4] - seg[lptr][3] < R - 1) or (seg[rptr][4] < R) ){
            rptr++;
            Ls.insert(seg[rptr][0]);
            Rs.insert(seg[rptr][1]);
            Sums.insert(seg[rptr][2]);

            //cerr<<"["<<seg[lptr][3]<<", "<<seg[rptr][4]<<"] "<<endl;
        }

        /// update ret
        //cerr<<"["<<lptr<<", "<<rptr<<"], ans = "<<max(*prev(Ls.end()) + *prev(Rs.end()), *prev(Sums.end()) )<<endl;
        ret = min(ret, max(*prev(Ls.end()) + *prev(Rs.end()), *prev(Sums.end()) ) );

        /// move lptr
        Ls.erase( Ls.find(seg[lptr][0]) );
        Rs.erase( Rs.find(seg[lptr][1]) );
        Sums.erase( Sums.find(seg[lptr][2]) );
        lptr++;

        if(seg[lptr][3] > 1 + dxr){
            break;
        }

    }

    return dxr + ret;

}

signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>R>>C;
    cin>>n;
    FOR(i, 1, n, 1){
        cin>>pt[i].F>>pt[i].S;
    }

    res = LNF;

    set<int> interesting;

    FOR(i, 1, n, 1){
        FOR(j, 1, n, 1){
            interesting.insert( max(abs(pt[j].F - pt[i].F) - 1, (int)0) );
            interesting.insert( pt[i].F - 1 + R - pt[j].F );
        }
    }

    for(int i : interesting){
        res = min(res, check(i));
    }

    cout<<res<<'\n';

    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 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 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 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 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 332 KB Output is correct
19 Correct 3 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 6 ms 332 KB Output is correct
22 Correct 14 ms 352 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 23 ms 368 KB Output is correct
25 Correct 17 ms 360 KB Output is correct
26 Correct 36 ms 388 KB Output is correct
27 Correct 36 ms 332 KB Output is correct
28 Correct 23 ms 376 KB Output is correct
29 Correct 34 ms 332 KB Output is correct
30 Correct 37 ms 392 KB Output is correct
31 Correct 39 ms 400 KB Output is correct
32 Correct 44 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 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 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 332 KB Output is correct
19 Correct 3 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 6 ms 332 KB Output is correct
22 Correct 14 ms 352 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 23 ms 368 KB Output is correct
25 Correct 17 ms 360 KB Output is correct
26 Correct 36 ms 388 KB Output is correct
27 Correct 36 ms 332 KB Output is correct
28 Correct 23 ms 376 KB Output is correct
29 Correct 34 ms 332 KB Output is correct
30 Correct 37 ms 392 KB Output is correct
31 Correct 39 ms 400 KB Output is correct
32 Correct 44 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 28 ms 332 KB Output is correct
35 Correct 40 ms 392 KB Output is correct
36 Correct 39 ms 396 KB Output is correct
37 Correct 34 ms 332 KB Output is correct
38 Correct 39 ms 320 KB Output is correct
39 Correct 39 ms 516 KB Output is correct
40 Correct 40 ms 404 KB Output is correct
41 Correct 29 ms 332 KB Output is correct
42 Correct 34 ms 412 KB Output is correct
43 Correct 38 ms 332 KB Output is correct
44 Correct 40 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 332 KB Output is correct
2 Correct 38 ms 332 KB Output is correct
3 Correct 35 ms 332 KB Output is correct
4 Correct 30 ms 332 KB Output is correct
5 Correct 33 ms 368 KB Output is correct
6 Correct 11 ms 332 KB Output is correct
7 Correct 3 ms 204 KB Output is correct
8 Correct 32 ms 332 KB Output is correct
9 Correct 17 ms 332 KB Output is correct
10 Correct 33 ms 372 KB Output is correct
11 Correct 34 ms 332 KB Output is correct
12 Correct 33 ms 332 KB Output is correct
13 Correct 6 ms 204 KB Output is correct
14 Correct 15 ms 344 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 34 ms 332 KB Output is correct
17 Correct 33 ms 332 KB Output is correct
18 Correct 15 ms 344 KB Output is correct
19 Correct 34 ms 332 KB Output is correct
20 Correct 29 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 332 KB Output is correct
2 Correct 38 ms 332 KB Output is correct
3 Correct 35 ms 332 KB Output is correct
4 Correct 30 ms 332 KB Output is correct
5 Correct 33 ms 368 KB Output is correct
6 Correct 11 ms 332 KB Output is correct
7 Correct 3 ms 204 KB Output is correct
8 Correct 32 ms 332 KB Output is correct
9 Correct 17 ms 332 KB Output is correct
10 Correct 33 ms 372 KB Output is correct
11 Correct 34 ms 332 KB Output is correct
12 Correct 33 ms 332 KB Output is correct
13 Correct 6 ms 204 KB Output is correct
14 Correct 15 ms 344 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 34 ms 332 KB Output is correct
17 Correct 33 ms 332 KB Output is correct
18 Correct 15 ms 344 KB Output is correct
19 Correct 34 ms 332 KB Output is correct
20 Correct 29 ms 332 KB Output is correct
21 Correct 1770 ms 776 KB Output is correct
22 Execution timed out 2072 ms 972 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 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 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 332 KB Output is correct
19 Correct 3 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 6 ms 332 KB Output is correct
22 Correct 14 ms 352 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 23 ms 368 KB Output is correct
25 Correct 17 ms 360 KB Output is correct
26 Correct 36 ms 388 KB Output is correct
27 Correct 36 ms 332 KB Output is correct
28 Correct 23 ms 376 KB Output is correct
29 Correct 34 ms 332 KB Output is correct
30 Correct 37 ms 392 KB Output is correct
31 Correct 39 ms 400 KB Output is correct
32 Correct 44 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 28 ms 332 KB Output is correct
35 Correct 40 ms 392 KB Output is correct
36 Correct 39 ms 396 KB Output is correct
37 Correct 34 ms 332 KB Output is correct
38 Correct 39 ms 320 KB Output is correct
39 Correct 39 ms 516 KB Output is correct
40 Correct 40 ms 404 KB Output is correct
41 Correct 29 ms 332 KB Output is correct
42 Correct 34 ms 412 KB Output is correct
43 Correct 38 ms 332 KB Output is correct
44 Correct 40 ms 320 KB Output is correct
45 Correct 10 ms 332 KB Output is correct
46 Correct 38 ms 332 KB Output is correct
47 Correct 35 ms 332 KB Output is correct
48 Correct 30 ms 332 KB Output is correct
49 Correct 33 ms 368 KB Output is correct
50 Correct 11 ms 332 KB Output is correct
51 Correct 3 ms 204 KB Output is correct
52 Correct 32 ms 332 KB Output is correct
53 Correct 17 ms 332 KB Output is correct
54 Correct 33 ms 372 KB Output is correct
55 Correct 34 ms 332 KB Output is correct
56 Correct 33 ms 332 KB Output is correct
57 Correct 6 ms 204 KB Output is correct
58 Correct 15 ms 344 KB Output is correct
59 Correct 8 ms 332 KB Output is correct
60 Correct 34 ms 332 KB Output is correct
61 Correct 33 ms 332 KB Output is correct
62 Correct 15 ms 344 KB Output is correct
63 Correct 34 ms 332 KB Output is correct
64 Correct 29 ms 332 KB Output is correct
65 Correct 1770 ms 776 KB Output is correct
66 Execution timed out 2072 ms 972 KB Time limit exceeded
67 Halted 0 ms 0 KB -