Submission #961034

# Submission time Handle Problem Language Result Execution time Memory
961034 2024-04-11T12:28:06 Z MarwenElarbi Ancient Books (IOI17_books) C++17
70 / 100
2000 ms 23040 KB
#include <bits/stdc++.h>
//#include "books.h"
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;
        cycle.clear();
        if(p[cur]!=cur){
            en=max(en,cur);
            bg=min(bg,cur);
        }
        if(vis[cur]) continue;
        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(curr<mr){
            curr++;
            mr=max(mr,r[curr]);
            ml=min(ml,l[curr]);
        }else if(curl>ml){
            curl--;
            mr=max(mr,r[curl]);
            ml=min(ml,l[curl]);
        }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 3 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8280 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 2 ms 8280 KB Output is correct
11 Correct 5 ms 8028 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 3 ms 8028 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8280 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 2 ms 8280 KB Output is correct
11 Correct 5 ms 8028 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 3 ms 8028 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 2 ms 8280 KB Output is correct
22 Correct 2 ms 8292 KB Output is correct
23 Correct 3 ms 8036 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8280 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8284 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 3 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8280 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 2 ms 8280 KB Output is correct
11 Correct 5 ms 8028 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 3 ms 8028 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 2 ms 8280 KB Output is correct
22 Correct 2 ms 8292 KB Output is correct
23 Correct 3 ms 8036 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8280 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 125 ms 23040 KB Output is correct
31 Correct 113 ms 19140 KB Output is correct
32 Correct 87 ms 17044 KB Output is correct
33 Correct 86 ms 16980 KB Output is correct
34 Correct 90 ms 16980 KB Output is correct
35 Correct 89 ms 16980 KB Output is correct
36 Correct 89 ms 17040 KB Output is correct
37 Correct 83 ms 16980 KB Output is correct
38 Correct 82 ms 17056 KB Output is correct
39 Correct 99 ms 16980 KB Output is correct
40 Correct 84 ms 17128 KB Output is correct
41 Correct 97 ms 18944 KB Output is correct
42 Correct 99 ms 17608 KB Output is correct
43 Correct 93 ms 17040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 3 ms 8280 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 8240 KB Output is correct
8 Correct 2 ms 8280 KB Output is correct
9 Correct 3 ms 8280 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 3 ms 8284 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8436 KB Output is correct
15 Correct 3 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 2 ms 8284 KB Output is correct
19 Correct 3 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8280 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 2 ms 8280 KB Output is correct
11 Correct 5 ms 8028 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 3 ms 8028 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 2 ms 8280 KB Output is correct
22 Correct 2 ms 8292 KB Output is correct
23 Correct 3 ms 8036 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8280 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 125 ms 23040 KB Output is correct
31 Correct 113 ms 19140 KB Output is correct
32 Correct 87 ms 17044 KB Output is correct
33 Correct 86 ms 16980 KB Output is correct
34 Correct 90 ms 16980 KB Output is correct
35 Correct 89 ms 16980 KB Output is correct
36 Correct 89 ms 17040 KB Output is correct
37 Correct 83 ms 16980 KB Output is correct
38 Correct 82 ms 17056 KB Output is correct
39 Correct 99 ms 16980 KB Output is correct
40 Correct 84 ms 17128 KB Output is correct
41 Correct 97 ms 18944 KB Output is correct
42 Correct 99 ms 17608 KB Output is correct
43 Correct 93 ms 17040 KB Output is correct
44 Correct 2 ms 8280 KB Output is correct
45 Correct 2 ms 8536 KB Output is correct
46 Correct 3 ms 8280 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 8240 KB Output is correct
51 Correct 2 ms 8280 KB Output is correct
52 Correct 3 ms 8280 KB Output is correct
53 Correct 2 ms 8284 KB Output is correct
54 Correct 3 ms 8284 KB Output is correct
55 Correct 3 ms 8284 KB Output is correct
56 Correct 2 ms 8284 KB Output is correct
57 Correct 2 ms 8436 KB Output is correct
58 Correct 3 ms 8284 KB Output is correct
59 Correct 2 ms 8284 KB Output is correct
60 Correct 2 ms 8284 KB Output is correct
61 Correct 2 ms 8284 KB Output is correct
62 Correct 3 ms 8284 KB Output is correct
63 Correct 2 ms 8284 KB Output is correct
64 Correct 82 ms 17032 KB Output is correct
65 Correct 92 ms 16976 KB Output is correct
66 Correct 82 ms 16976 KB Output is correct
67 Correct 92 ms 17032 KB Output is correct
68 Correct 10 ms 9048 KB Output is correct
69 Correct 10 ms 9052 KB Output is correct
70 Correct 10 ms 9052 KB Output is correct
71 Correct 10 ms 9100 KB Output is correct
72 Correct 10 ms 9052 KB Output is correct
73 Correct 10 ms 9052 KB Output is correct
74 Correct 85 ms 17040 KB Output is correct
75 Correct 98 ms 17048 KB Output is correct
76 Correct 83 ms 16980 KB Output is correct
77 Correct 83 ms 17040 KB Output is correct
78 Correct 114 ms 17104 KB Output is correct
79 Correct 103 ms 17044 KB Output is correct
80 Correct 76 ms 16976 KB Output is correct
81 Execution timed out 2061 ms 16980 KB Time limit exceeded
82 Halted 0 ms 0 KB -