Submission #835544

# Submission time Handle Problem Language Result Execution time Memory
835544 2023-08-23T15:38:25 Z jasmin Ancient Books (IOI17_books) C++17
50 / 100
186 ms 21308 KB
//#include "books.h"
#include<bits/stdc++.h>
using namespace std;
 
const int INF=1e9+7;
 
long long minimum_walk(vector<int> p, int s) {
    int n=p.size();
	
    long long ans=0;
    
    vector<bool> vis(n, false);
    vector<pair<int,int> > ranges;
    vector<bool> ends(n);
    vector<bool> starts(n);
    vector<int> cycle(n);
    for(int i=0; i<n; i++){
        if(vis[i]) continue;
 
        int v=i;
        int cnt=0;
        int l=i;
        int r=i;
        while(!vis[v]){
            vis[v]=true;
            cycle[v] = i;
 
            cnt++;
            l=min(l, v);
            r=max(r, v);
 
            ans += abs(p[v]-v);
            v=p[v];
        }
        ends[r] = true;
        starts[l] = true;
 
        if(cnt!=1 || v==s){
            ranges.push_back({l, r});
        }
    }

    if(ranges.empty()){
        return 0;
    }
    sort(ranges.begin(), ranges.end());

 
    int last=ranges[0].first;
    for(auto [l, r]: ranges){
 
        if(last < l){
            ans += 2*(l-last);
        }
 
        last=max(last, r);
    }

    //cout << "zwischenres " << ans << "\n" << flush;

    //start: 
    int left=INF;
    int right=-1; int rightborder=-1;
    for(auto [l, r]: ranges){
        if(r<s || s<l) continue;

        left = min(left, l);
        if(rightborder < r){
            rightborder = r;
            right = l;
        }
    }


    set<int> active;
    set<int> reachl;
    for(int i=0; i<n; i++){

        if(starts[i]){
            active.insert(cycle[i]);
        }

        if(cycle[i]==left){
            for(auto e: active){
                reachl.insert(e);
            }
            active.clear();
        }

        if(ends[i]){
            active.erase(cycle[i]);
        }
    }
    active.clear();
    set<int> reachr;
    for(int i=0; i<n; i++){

        if(starts[i]){
            active.insert(cycle[i]);
        }

        if(cycle[i]==right){
            for(auto e: active){
                reachr.insert(e);
            }
            active.clear();
        }

        if(ends[i]){
            active.erase(cycle[i]);
        }
    }


    vector<bool> good(n);
    if(reachl.find(right)!=reachl.end()){

        for(auto e: reachr){
            good[e]=true;
        }

    }
    if(reachr.find(left)!=reachr.end()){

        for(auto e: reachl){
            good[e]=true;
        }
    }

    int mindist=INF;
    for(int i=0; i<n; i++){

        if(good[cycle[i]]){
            mindist = min(mindist, abs(s-i));
        }
    }
    ans += 2*mindist;
 
    return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 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 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 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 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 175 ms 12520 KB Output is correct
31 Correct 124 ms 12400 KB Output is correct
32 Correct 134 ms 12404 KB Output is correct
33 Correct 135 ms 14496 KB Output is correct
34 Correct 138 ms 21200 KB Output is correct
35 Correct 136 ms 21212 KB Output is correct
36 Correct 123 ms 21308 KB Output is correct
37 Correct 90 ms 19420 KB Output is correct
38 Correct 97 ms 19292 KB Output is correct
39 Correct 82 ms 19292 KB Output is correct
40 Correct 106 ms 19168 KB Output is correct
41 Correct 103 ms 19160 KB Output is correct
42 Correct 112 ms 19160 KB Output is correct
43 Correct 186 ms 21216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 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 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 175 ms 12520 KB Output is correct
31 Correct 124 ms 12400 KB Output is correct
32 Correct 134 ms 12404 KB Output is correct
33 Correct 135 ms 14496 KB Output is correct
34 Correct 138 ms 21200 KB Output is correct
35 Correct 136 ms 21212 KB Output is correct
36 Correct 123 ms 21308 KB Output is correct
37 Correct 90 ms 19420 KB Output is correct
38 Correct 97 ms 19292 KB Output is correct
39 Correct 82 ms 19292 KB Output is correct
40 Correct 106 ms 19168 KB Output is correct
41 Correct 103 ms 19160 KB Output is correct
42 Correct 112 ms 19160 KB Output is correct
43 Correct 186 ms 21216 KB Output is correct
44 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
45 Halted 0 ms 0 KB -