답안 #719628

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719628 2023-04-06T11:54:25 Z Jarif_Rahman 고대 책들 (IOI17_books) C++17
42 / 100
2000 ms 127752 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;
    dsu(int nn){
        n = nn;
        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;
        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++;
    }

    //for(int x: C) cerr << x << " "; cerr << "C1\n";

    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;
    }

    vector<set<int>> sth(k);
    for(int i = 0; i < n; i++) sth[C[i]].insert(i);

    dsu ds(k);
    for(int i = 0; i < k; i++) for(int j = i+1; j < k; j++){
        if(unnecessary[i] || unnecessary[j]) continue;
        if(last[i] < first[j] || last[j] < first[i]) continue;
        auto it = sth[i].upper_bound(last[j]);
        if(it != sth[i].end() && it != sth[i].begin() && *prev(it) < first[j]) continue;
        it = sth[j].upper_bound(last[i]);
        if(it != sth[j].end() && it != sth[j].begin() && *prev(it) < first[i]) continue;
        ds.unite(i, j);
    }

    for(int &x: C) x = ds.get(x);
    first.assign(k, -1);
    for(int i = 0; i < n; i++){
        if(first[C[i]] == -1) first[C[i]] = i;
        last[C[i]] = i;
    }

    //for(int x: C) cerr << x << " "; cerr << "C2\n";

    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 296 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 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 296 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 212 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 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 296 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 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 296 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 212 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 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 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 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 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 296 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 212 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 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 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 412 ms 59520 KB Output is correct
31 Correct 404 ms 61048 KB Output is correct
32 Execution timed out 2083 ms 127752 KB Time limit exceeded
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 2 ms 420 KB Output is correct
11 Correct 2 ms 296 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 2 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 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 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 296 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 212 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 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 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 412 ms 59520 KB Output is correct
31 Correct 404 ms 61048 KB Output is correct
32 Execution timed out 2083 ms 127752 KB Time limit exceeded
33 Halted 0 ms 0 KB -