답안 #719663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719663 2023-04-06T13:05:24 Z Jarif_Rahman 고대 책들 (IOI17_books) C++17
100 / 100
500 ms 46264 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

struct dsu{
    int n;
    vector<int> sz, p;
    vector<int> &first, &last;
    dsu(int _n, vector<int>& _first, vector<int>& _last): n(_n), first(_first), last(_last){
        sz.resize(n, 1);
        p.resize(n);
        for(int i = 0; i < n; i++) p[i] = i;
    }
    int get(int x){
        if(p[x] != x) p[x] = get(p[x]);
        return p[x];
    }
    void unite(int a, int b){
        a = get(a), b  = get(b);
        if(a == b) return;
        if(sz[b] > sz[a]) swap(a, b);
        sz[a]+=sz[b];
        sz[b] = 0;
        first[a] = min(first[a], first[b]);
        last[a] = max(last[a], last[b]);
        p[b] = a;
    }
};
 
ll minimum_walk(vector<int> p, int s){
    int n = p.size();
    ll ans = 0;
 
    vector<int> C(n, -1), sz;
    int k = 0;
    for(int i = 0; i < n; i++){
        if(C[i] != -1) continue;
        int x = p[i];
        C[i] = k;
        sz.pb(1);
        ans+=abs(i-x);
        while(x != i){
            C[x] = k;
            sz.back()++;
            ans+=abs(p[x]-x);
            x = p[x];
        }
        k++;
    }

    vector<int> first(k, -1), last(k);
    for(int i = 0; i < n; i++){
        if(first[C[i]] == -1) first[C[i]] = i;
        last[C[i]] = i;
    }

    int L, R;
    vector<bool> unnecessary(k, 0);
    for(int i = 0; i < n; i++){
        if(sz[C[i]] > 1 || i == s){
            L = i;
            break;
        }
        unnecessary[C[i]] = 1;
    }
    for(int i = n-1; i >= 0; i--){
        if(sz[C[i]] > 1 || i == s){
            R = i;
            break;
        }
        unnecessary[C[i]] = 1;
    }

    dsu ds(k, first, last);
    vector<int> last_added(k, -1);
    set<int> st;
    for(int i = 0; i < n; i++){
        int c = ds.get(C[i]);
        if(last_added[c] != -1) st.erase(last_added[c]), last_added[c] = -1;
        while(!st.empty() && *prev(st.end()) > first[c]){
            int j = *prev(st.end());
            st.erase(prev(st.end()));
            last_added[ds.get(C[j])] = -1;
            ds.unite(c, C[j]);
        }
        c = ds.get(c);
        if(i == last[c]) continue;
        last_added[c] = i;
        st.insert(i);
    }
    for(int i = n-1; i >= 0; i--){
        int c = ds.get(C[i]);
        if(last_added[c] != -1) st.erase(last_added[c]), last_added[c] = -1;
        while(!st.empty() && *st.begin() < last[c]){
            int j = *st.begin();
            st.erase(st.begin());
            last_added[ds.get(C[j])] = -1;
            ds.unite(c, C[j]);
        }
        c = ds.get(c);
        if(i == first[c]) continue;
        last_added[c] = i;
        st.insert(i);
    }

    for(int &x: C) x = ds.get(x);

    int l = first[C[s]], r = last[C[s]];
    while(l > L || r < R){
        int in = -1;
        ll lx = 0, rx = 0;
        for(int i = l-1; i >= 0; i--){
            if(unnecessary[C[i]]) break;
            if(C[i] == C[i+1]) continue;
            lx+=2;
            if(i != last[C[i]]){
                in = i;
                break;
            }
            i = first[C[i]];
        }
        for(int i = r+1; i < n; i++){
            if(unnecessary[C[i]]) break;
            if(C[i] == C[i-1]) continue;
            rx+=2;
            if(i != first[C[i]]){
                in = i;
                break;
            }
            i = last[C[i]];
        }

        if(in == -1){
            ans+=lx+rx;
            break;
        }
        ans+=min(lx, rx);
        l = first[C[in]], r = last[C[in]];
    }

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
30 Correct 260 ms 12352 KB Output is correct
31 Correct 246 ms 12320 KB Output is correct
32 Correct 116 ms 35836 KB Output is correct
33 Correct 162 ms 28644 KB Output is correct
34 Correct 155 ms 28636 KB Output is correct
35 Correct 184 ms 25300 KB Output is correct
36 Correct 196 ms 18528 KB Output is correct
37 Correct 184 ms 13084 KB Output is correct
38 Correct 182 ms 12464 KB Output is correct
39 Correct 199 ms 12392 KB Output is correct
40 Correct 182 ms 12324 KB Output is correct
41 Correct 198 ms 12184 KB Output is correct
42 Correct 195 ms 12400 KB Output is correct
43 Correct 185 ms 29752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
30 Correct 260 ms 12352 KB Output is correct
31 Correct 246 ms 12320 KB Output is correct
32 Correct 116 ms 35836 KB Output is correct
33 Correct 162 ms 28644 KB Output is correct
34 Correct 155 ms 28636 KB Output is correct
35 Correct 184 ms 25300 KB Output is correct
36 Correct 196 ms 18528 KB Output is correct
37 Correct 184 ms 13084 KB Output is correct
38 Correct 182 ms 12464 KB Output is correct
39 Correct 199 ms 12392 KB Output is correct
40 Correct 182 ms 12324 KB Output is correct
41 Correct 198 ms 12184 KB Output is correct
42 Correct 195 ms 12400 KB Output is correct
43 Correct 185 ms 29752 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 300 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 296 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 296 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 159 ms 36052 KB Output is correct
65 Correct 153 ms 37876 KB Output is correct
66 Correct 243 ms 19556 KB Output is correct
67 Correct 219 ms 19168 KB Output is correct
68 Correct 20 ms 2644 KB Output is correct
69 Correct 22 ms 2388 KB Output is correct
70 Correct 20 ms 3024 KB Output is correct
71 Correct 17 ms 3532 KB Output is correct
72 Correct 19 ms 4172 KB Output is correct
73 Correct 21 ms 1976 KB Output is correct
74 Correct 161 ms 35160 KB Output is correct
75 Correct 155 ms 35156 KB Output is correct
76 Correct 187 ms 35916 KB Output is correct
77 Correct 195 ms 36380 KB Output is correct
78 Correct 200 ms 33892 KB Output is correct
79 Correct 196 ms 33744 KB Output is correct
80 Correct 122 ms 42324 KB Output is correct
81 Correct 500 ms 46264 KB Output is correct
82 Correct 442 ms 46152 KB Output is correct
83 Correct 293 ms 29132 KB Output is correct
84 Correct 304 ms 26844 KB Output is correct
85 Correct 304 ms 22364 KB Output is correct
86 Correct 290 ms 20060 KB Output is correct
87 Correct 227 ms 46036 KB Output is correct
88 Correct 242 ms 39752 KB Output is correct
89 Correct 287 ms 31548 KB Output is correct
90 Correct 260 ms 19296 KB Output is correct
91 Correct 239 ms 18892 KB Output is correct
92 Correct 224 ms 18808 KB Output is correct