답안 #801622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
801622 2023-08-02T06:58:58 Z 반딧불(#10089) Cultivation (JOI17_cultivation) C++17
65 / 100
558 ms 1424 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int INF = 2e9;

ll n, m; int k;
ll X[302], Y[302];
int rx[302], ry[302], sx, sy;
ll ans = LLONG_MAX;

struct Event{
    ll x, y, v;
    Event(){}
    Event(ll x, ll y, ll v): x(x), y(y), v(v){}
    bool operator<(const Event &r)const{
        if(x!=r.x) return x<r.x;
        return v > r.v;
    }
} vec[1002];

struct Interval{
    ll l, r; /// ���� ����
    ll ldist, rdist, mdist; /// �ּ� ����
    Interval(){}
    Interval(ll l, ll r, ll ldist, ll rdist, ll mdist): l(l), r(r), ldist(ldist), rdist(rdist), mdist(mdist){}
    bool operator<(const Interval &nxt)const{
        return l<nxt.l;
    }
};

pair<ll, int> dqL[1002], dqR[1002], dqLen[1002];
int l1, l2, r1, r2, len1, len2;

Interval info[1002];
int infoCnt;

ll udlength[500002], udsize;

int main(){
    scanf("%lld %lld %d", &n, &m, &k);
//    n=m=1000000000, k=300;
    for(int i=1; i<=k; i++){
        scanf("%lld %lld", &X[i], &Y[i]);
//        X[i] = Y[i] = i*i+i+2;
    }

    udlength[udsize++] = n-1;
    for(int i=1; i<=k; i++){
        udlength[udsize++] = X[i] - 1;
        udlength[udsize++] = n - X[i];
        for(int j=1; j<=k; j++){
            if(X[i] < X[j]) udlength[udsize++] = X[j] - X[i] - 1;
            udlength[udsize++] = X[i] + n - X[j] - 1;
        }
    }
    sort(udlength, udlength+udsize);
    udsize = unique(udlength, udlength+udsize) - udlength;

    for(int ui=0; ui<udsize; ui++){ /// �� - �Ʒ� ����
        ll ud = udlength[ui];
        if(ud<0) continue;

        for(int i=1; i<=k; i++){
            vec[i*2-2] = Event(X[i], Y[i], 1);
            vec[i*2-1] = Event(X[i]+ud+1, Y[i], -1);
        }
        sort(vec, vec+k+k);

        infoCnt = 0;
        multiset<ll> mst, diffs;
        bool bad = 0;

        for(int s=0; s<k+k; s++){
            int e = s;
            while(e+1 < k+k && vec[e+1].x == vec[s].x) e++;
            if(e+1 == k+k) break;

            /// [s, e] ���� �������� ó��
            for(int i=s; i<=e; i++){
                if(vec[i].v == 1){
                    auto it = mst.insert(vec[i].y);
                    if(next(it) != mst.end() && it != mst.begin()) diffs.erase(*next(it) - *prev(it) - 1);
                    if(it != mst.begin()) diffs.insert(vec[i].y - *prev(it) - 1);
                    if(next(it) != mst.end()) diffs.insert(*next(it) - vec[i].y - 1);
                }
                else{
                    auto it = mst.find(vec[i].y);
                    if(next(it) != mst.end() && it != mst.begin()) diffs.insert(*next(it) - *prev(it) - 1);
                    if(it != mst.begin()) diffs.erase(vec[i].y - *prev(it) - 1);
                    if(next(it) != mst.end()) diffs.erase(*next(it) - vec[i].y - 1);
                    mst.erase(it);
                }
            }

            /// ��� ������ �Ѿ
            if(mst.empty()){
                bad = 1;
                break;
            }

            info[infoCnt++] = Interval(vec[s].x, vec[e+1].x-1, *mst.begin() - 1, m - *mst.rbegin(), diffs.empty() ? 0 : *diffs.rbegin());
            s = e;
        }
        if(bad || !infoCnt) continue;

        int j = 0;
        l1 = l2 = r1 = r2 = len1 = len2 = 0;
        dqL[l2++] = make_pair(info[0].ldist, 0);
        dqR[r2++] = make_pair(info[0].rdist, 0);
        dqLen[len2++] = make_pair(info[0].mdist, 0);

        for(int i=0; i<infoCnt; i++){
            if(l1!=l2 && dqL[l1].second < i) l1++;
            if(r1!=r2 && dqR[r1].second < i) r1++;
            if(len1!=len2 && dqLen[len1].second < i) len1++;
            while(j < infoCnt && info[j].r - info[i].l + 1 < n){
                j++;
                while(l1!=l2 && dqL[l2-1].first <= info[j].ldist) l2--;
                dqL[l2++] = make_pair(info[j].ldist, j);
                while(r1!=r2 && dqR[r2-1].first <= info[j].rdist) r2--;
                dqR[r2++] = make_pair(info[j].rdist, j);
                while(len1!=len2 && dqLen[len2-1].first <= info[j].mdist) len2--;
                dqLen[len2++] = make_pair(info[j].mdist, j);
            }
            if(j == infoCnt) break;
            ll minL = dqL[l1].first, minR = dqR[r1].first, len = dqLen[len1].first;
            ans = min(ans, ud + max(len, minL+minR));
        }
    }

    printf("%lld", ans);
}

Compilation message

cultivation.cpp: In function 'int main()':
cultivation.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     scanf("%lld %lld %d", &n, &m, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         scanf("%lld %lld", &X[i], &Y[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 6 ms 520 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 10 ms 724 KB Output is correct
25 Correct 7 ms 596 KB Output is correct
26 Correct 17 ms 1404 KB Output is correct
27 Correct 19 ms 1364 KB Output is correct
28 Correct 10 ms 828 KB Output is correct
29 Correct 16 ms 1424 KB Output is correct
30 Correct 19 ms 1364 KB Output is correct
31 Correct 17 ms 1404 KB Output is correct
32 Correct 16 ms 1420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 6 ms 520 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 10 ms 724 KB Output is correct
25 Correct 7 ms 596 KB Output is correct
26 Correct 17 ms 1404 KB Output is correct
27 Correct 19 ms 1364 KB Output is correct
28 Correct 10 ms 828 KB Output is correct
29 Correct 16 ms 1424 KB Output is correct
30 Correct 19 ms 1364 KB Output is correct
31 Correct 17 ms 1404 KB Output is correct
32 Correct 16 ms 1420 KB Output is correct
33 Correct 3 ms 1236 KB Output is correct
34 Correct 18 ms 1364 KB Output is correct
35 Incorrect 20 ms 1416 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 6 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 7 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 6 ms 340 KB Output is correct
11 Correct 7 ms 340 KB Output is correct
12 Correct 8 ms 348 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 6 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 7 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 6 ms 340 KB Output is correct
11 Correct 7 ms 340 KB Output is correct
12 Correct 8 ms 348 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 323 ms 456 KB Output is correct
22 Correct 557 ms 576 KB Output is correct
23 Correct 520 ms 444 KB Output is correct
24 Correct 367 ms 460 KB Output is correct
25 Correct 542 ms 444 KB Output is correct
26 Correct 205 ms 444 KB Output is correct
27 Correct 355 ms 456 KB Output is correct
28 Correct 536 ms 444 KB Output is correct
29 Correct 498 ms 444 KB Output is correct
30 Correct 530 ms 340 KB Output is correct
31 Correct 558 ms 444 KB Output is correct
32 Correct 502 ms 448 KB Output is correct
33 Correct 305 ms 448 KB Output is correct
34 Correct 494 ms 444 KB Output is correct
35 Correct 482 ms 444 KB Output is correct
36 Correct 508 ms 448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 6 ms 520 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 10 ms 724 KB Output is correct
25 Correct 7 ms 596 KB Output is correct
26 Correct 17 ms 1404 KB Output is correct
27 Correct 19 ms 1364 KB Output is correct
28 Correct 10 ms 828 KB Output is correct
29 Correct 16 ms 1424 KB Output is correct
30 Correct 19 ms 1364 KB Output is correct
31 Correct 17 ms 1404 KB Output is correct
32 Correct 16 ms 1420 KB Output is correct
33 Correct 3 ms 1236 KB Output is correct
34 Correct 18 ms 1364 KB Output is correct
35 Incorrect 20 ms 1416 KB Output isn't correct
36 Halted 0 ms 0 KB -