Submission #961043

# Submission time Handle Problem Language Result Execution time Memory
961043 2024-04-11T12:40:29 Z MarwenElarbi Ancient Books (IOI17_books) C++17
70 / 100
2000 ms 21700 KB
#include <bits/stdc++.h>
#include "books.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
bool vis[1000005];
vector<int> l(1000000),r(1000000);
long long minimum_walk(std::vector<int> p, int s){
    int n=p.size();
    long long ans=0;
    int en=s;
    int bg=s;
    //cout << ans<<endl;
    vector<int> cycle;
    for (int i = 0; i < n; ++i)
    {
        ans+=abs(p[i]-i);
        int cur=i;
        if(p[cur]!=cur){
            en=max(en,cur);
            bg=min(bg,cur);
        }
        if(vis[cur]) continue;
        cycle.clear();
        int mn=1e6+5;
        int mx=-1;
        while(vis[cur]==0){
            cycle.push_back(cur);
            vis[cur]=1;
            mx=max(mx,cur);
            mn=min(mn,cur);
            cur=p[cur];
        }
        for(int u:cycle){
            //cout <<u<<" ";
            l[u]=mn;
            r[u]=mx;
        } 
    }
    //cout <<"nabba"<<endl;
    int curl=s;int ml=l[s];
    int curr=s;int mr=r[s];
    while(curr<en||curl>bg){
        //cout <<curl<<" "<<curr<<endl;
        if(curl>ml){
            curl--;
            mr=max(mr,r[curl]);
            ml=min(ml,l[curl]);
        }else if(curr<mr){
            curr++;
            mr=max(mr,r[curr]);
            ml=min(ml,l[curr]);
        }else{
            //cout <<"hey"<<endl;
            bool ok=0;
            int costl=0;
            int x=mr;
            int cl=mr;
            while(x>bg){
                cl=min(cl,l[x]);
                if(x==cl){
                    costl+=2;
                    cl--;
                }
                x--;
                if(r[x]>mr){
                    ok=true;
                    break;
                }
            }
            x=ml;
            int cr=ml;
            int costr=0;
            while(x<en){
                cr=max(cr,r[x]);
                if(x==cr){
                    cr++;
                    costr+=2;
                }
                x++;
                if(l[x]<ml){
                    ok=true;
                    break;
                }
            }
            if(ok){
                if(costl>costr){
                    mr=cr;
                    ans+=costr;
                }else{
                    ml=cl;
                    ans+=costl;
                }
            }else{
                ans+=costl+costr;
                break;
            }
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8024 KB Output is correct
2 Correct 3 ms 8024 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 6 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 3 ms 8280 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 5 ms 8292 KB Output is correct
16 Correct 3 ms 8280 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 2 ms 8096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8024 KB Output is correct
2 Correct 3 ms 8024 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 6 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 3 ms 8280 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 5 ms 8292 KB Output is correct
16 Correct 3 ms 8280 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 2 ms 8096 KB Output is correct
19 Correct 4 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8028 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8024 KB Output is correct
2 Correct 3 ms 8024 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 6 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 3 ms 8280 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 5 ms 8292 KB Output is correct
16 Correct 3 ms 8280 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 2 ms 8096 KB Output is correct
19 Correct 4 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8028 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 104 ms 21700 KB Output is correct
31 Correct 120 ms 19144 KB Output is correct
32 Correct 109 ms 17044 KB Output is correct
33 Correct 91 ms 17036 KB Output is correct
34 Correct 84 ms 17036 KB Output is correct
35 Correct 85 ms 17036 KB Output is correct
36 Correct 94 ms 17084 KB Output is correct
37 Correct 80 ms 16976 KB Output is correct
38 Correct 105 ms 17040 KB Output is correct
39 Correct 85 ms 16976 KB Output is correct
40 Correct 91 ms 17168 KB Output is correct
41 Correct 100 ms 18808 KB Output is correct
42 Correct 98 ms 17744 KB Output is correct
43 Correct 86 ms 17036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8036 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 2 ms 8164 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 2 ms 8284 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 3 ms 8532 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 4 ms 8280 KB Output is correct
11 Correct 3 ms 8536 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 2 ms 8092 KB Output is correct
14 Correct 3 ms 8440 KB Output is correct
15 Correct 3 ms 8284 KB Output is correct
16 Correct 2 ms 8280 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 3 ms 8284 KB Output is correct
19 Correct 2 ms 8280 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8024 KB Output is correct
2 Correct 3 ms 8024 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 6 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 3 ms 8280 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 5 ms 8292 KB Output is correct
16 Correct 3 ms 8280 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 2 ms 8096 KB Output is correct
19 Correct 4 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8028 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 104 ms 21700 KB Output is correct
31 Correct 120 ms 19144 KB Output is correct
32 Correct 109 ms 17044 KB Output is correct
33 Correct 91 ms 17036 KB Output is correct
34 Correct 84 ms 17036 KB Output is correct
35 Correct 85 ms 17036 KB Output is correct
36 Correct 94 ms 17084 KB Output is correct
37 Correct 80 ms 16976 KB Output is correct
38 Correct 105 ms 17040 KB Output is correct
39 Correct 85 ms 16976 KB Output is correct
40 Correct 91 ms 17168 KB Output is correct
41 Correct 100 ms 18808 KB Output is correct
42 Correct 98 ms 17744 KB Output is correct
43 Correct 86 ms 17036 KB Output is correct
44 Correct 2 ms 8036 KB Output is correct
45 Correct 3 ms 8284 KB Output is correct
46 Correct 2 ms 8164 KB Output is correct
47 Correct 2 ms 8284 KB Output is correct
48 Correct 2 ms 8284 KB Output is correct
49 Correct 2 ms 8284 KB Output is correct
50 Correct 3 ms 8532 KB Output is correct
51 Correct 3 ms 8284 KB Output is correct
52 Correct 2 ms 8284 KB Output is correct
53 Correct 4 ms 8280 KB Output is correct
54 Correct 3 ms 8536 KB Output is correct
55 Correct 3 ms 8284 KB Output is correct
56 Correct 2 ms 8092 KB Output is correct
57 Correct 3 ms 8440 KB Output is correct
58 Correct 3 ms 8284 KB Output is correct
59 Correct 2 ms 8280 KB Output is correct
60 Correct 3 ms 8284 KB Output is correct
61 Correct 3 ms 8284 KB Output is correct
62 Correct 2 ms 8280 KB Output is correct
63 Correct 2 ms 8284 KB Output is correct
64 Correct 84 ms 17040 KB Output is correct
65 Correct 87 ms 17036 KB Output is correct
66 Correct 88 ms 17040 KB Output is correct
67 Correct 84 ms 17036 KB Output is correct
68 Correct 10 ms 9052 KB Output is correct
69 Correct 11 ms 8932 KB Output is correct
70 Correct 11 ms 9052 KB Output is correct
71 Correct 11 ms 9052 KB Output is correct
72 Correct 11 ms 9048 KB Output is correct
73 Correct 10 ms 9052 KB Output is correct
74 Correct 85 ms 17036 KB Output is correct
75 Correct 83 ms 17056 KB Output is correct
76 Correct 91 ms 16980 KB Output is correct
77 Correct 90 ms 17036 KB Output is correct
78 Correct 87 ms 17048 KB Output is correct
79 Correct 87 ms 16908 KB Output is correct
80 Correct 79 ms 17056 KB Output is correct
81 Execution timed out 2021 ms 16976 KB Time limit exceeded
82 Halted 0 ms 0 KB -