Submission #1058956

# Submission time Handle Problem Language Result Execution time Memory
1058956 2024-08-14T15:21:38 Z shenfe1 Ancient Books (IOI17_books) C++17
100 / 100
238 ms 129112 KB
#include <bits/stdc++.h>
#include "books.h"

#pragma GCC optimize("Ofast")

using namespace std;

#define pb push_back
#define pii pair<int,int>
#define sz(s) (int)s.size()
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define ll long long
#define lb lower_bound

const int MAX=1e6+10;
const ll inf=1e18;

int n;
vector<int> g[MAX];
bool use[MAX];
vector<int> vec;
int pref[MAX];

struct dsu{
    int f[MAX];

    void init(int n){
        for(int i=0;i<n;i++)f[i]=i;
    }

    int get(int v){
        return (f[v]==v?v:f[v]=get(f[v]));
    }

    void unite(int a,int b){
        a=get(a),b=get(b);
        f[a]=b;
    }
}d;

int num[MAX];
int prefz[MAX];

bool is(int l,int r,vector<int> x){
    for(int pos:x){
        if(l<=pos&&pos<=r)return 1;
    }
    return 0;
}

int get(int l,int r){
    if(l==0)return prefz[r];
    return prefz[r]-prefz[l-1];
}

ll minimum_walk(vector<int> p,int s){
    n=sz(p);
    int Lneed=0,Rneed=n-1;
    ll ans=0;
    for(int i=0;i<n;i++){
        ans+=abs(i-p[i]);
    }
    vector<vector<int>> x;
    vector<pii> seg,bds;
    for(int i=0;i<n;i++){
        if(!use[i]){
            vec.clear();
            int v=i;
            while(!use[v]){
                num[v]=sz(seg);
                vec.pb(v);
                use[v]=1;
                v=p[v];
            }
            sort(all(vec));
            seg.pb({vec[0],vec.back()});
            int l=0,r=n-1;
            for(int f:vec){
                if(f<=s)l=f;
                else{
                    r=f;
                    break;
                }
            }
            bds.pb({l,r});
            x.pb(vec);
        }
    }
    d.init(n);
    vector<int> pref(n,0);
    for(int i=0;i<sz(seg);i++){
        auto &[l,r]=seg[i];
        if(l<=s&&s<=r&&i!=num[s]){
            continue;
        }
        pref[l]++;
        pref[r]--;
    }
    for(int i=1;i<n;i++)pref[i]+=pref[i-1];
    prefz[0]=(pref[0]==0);
    for(int i=1;i<n;i++){
        // if(pref[i]==0)cout<<i<<"\n";
        prefz[i]=prefz[i-1]+(pref[i]==0);
    }
    int cur=0;
    int L=s,R=s;
    set<pii> sl,sr;
    for(int i=0;i<sz(seg);i++){
        auto [l,r]=seg[i];
        if(l<s&&s<r&&i!=num[s]){
            // cout<<i<<"\n";
            // cout<<l<<" "<<r<<"\n";
            sl.insert({bds[i].F,i});
            sr.insert({bds[i].S,i});
        }
    }
    while(R-L+1!=n){
        while(L-1>=0&&pref[L-1]!=0)L--;
        while(R+1<n&&pref[R]!=0)R++;
        bool ok=0;
        // cout<<L<<" "<<R<<"\n";
        while(!sl.empty()&&L<=sl.rbegin()->F&&sl.rbegin()->F<=R){
            ok=1;
            int i=sl.rbegin()->S;
            sl.erase(--sl.end());
            sr.erase({bds[i].S,i});
            L=min(L,seg[i].F);
            R=max(R,seg[i].S);
        }
        while(!sr.empty()&&L<=sr.begin()->F&&sr.begin()->F<=R){
            ok=1;
            int i=sr.begin()->S;
            sr.erase(sr.begin());
            sl.erase({bds[i].F,i});
            L=min(L,seg[i].F);
            R=max(R,seg[i].S);
        }
        if(!ok){
            int mn=n+10,pos=-1;
            if(!sl.empty()){
                if(get(sl.rbegin()->F,L-1)<mn){
                    mn=get(sl.rbegin()->F,L-1);
                    pos=sl.rbegin()->S;
                }
            }
            if(!sr.empty()){
                if(get(R,sr.begin()->F-1)<mn){
                    mn=get(R,sr.begin()->F-1);
                    pos=sr.begin()->S;
                }
            }
            if(pos!=-1){
                ok=1;
                ans+=2*mn;
                L=seg[pos].F,R=seg[pos].S;
                sl.erase({bds[pos].F,pos});
                sr.erase({bds[pos].S,pos});
            }
        }
        while(L-1>=0&&pref[L-1]!=0)L--;
        while(R+1<n&&pref[R]!=0)R++;
        if(!ok){
            if(L-1>=0){
                L--;
                ans+=2;
            }
            if(R+1<n){
                R++;
                ans+=2;
            }
        }
    }
    for(int i=0;i<s;i++){
        if(p[i]==i)ans-=2;
        else break;
    }
    for(int i=n-1;i>s;i--){
        if(p[i]==i)ans-=2;
        else break;
    }

    return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:60:9: warning: unused variable 'Lneed' [-Wunused-variable]
   60 |     int Lneed=0,Rneed=n-1;
      |         ^~~~~
books.cpp:60:17: warning: unused variable 'Rneed' [-Wunused-variable]
   60 |     int Lneed=0,Rneed=n-1;
      |                 ^~~~~
books.cpp:107:9: warning: unused variable 'cur' [-Wunused-variable]
  107 |     int cur=0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 23900 KB Output is correct
2 Correct 8 ms 23832 KB Output is correct
3 Correct 8 ms 23900 KB Output is correct
4 Correct 7 ms 23900 KB Output is correct
5 Correct 8 ms 23900 KB Output is correct
6 Correct 8 ms 23984 KB Output is correct
7 Correct 7 ms 23996 KB Output is correct
8 Correct 8 ms 23900 KB Output is correct
9 Correct 7 ms 23900 KB Output is correct
10 Correct 7 ms 24004 KB Output is correct
11 Correct 8 ms 23784 KB Output is correct
12 Correct 8 ms 23872 KB Output is correct
13 Correct 8 ms 23896 KB Output is correct
14 Correct 8 ms 23900 KB Output is correct
15 Correct 8 ms 23988 KB Output is correct
16 Correct 7 ms 23896 KB Output is correct
17 Correct 7 ms 23900 KB Output is correct
18 Correct 8 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 23900 KB Output is correct
2 Correct 8 ms 23832 KB Output is correct
3 Correct 8 ms 23900 KB Output is correct
4 Correct 7 ms 23900 KB Output is correct
5 Correct 8 ms 23900 KB Output is correct
6 Correct 8 ms 23984 KB Output is correct
7 Correct 7 ms 23996 KB Output is correct
8 Correct 8 ms 23900 KB Output is correct
9 Correct 7 ms 23900 KB Output is correct
10 Correct 7 ms 24004 KB Output is correct
11 Correct 8 ms 23784 KB Output is correct
12 Correct 8 ms 23872 KB Output is correct
13 Correct 8 ms 23896 KB Output is correct
14 Correct 8 ms 23900 KB Output is correct
15 Correct 8 ms 23988 KB Output is correct
16 Correct 7 ms 23896 KB Output is correct
17 Correct 7 ms 23900 KB Output is correct
18 Correct 8 ms 23900 KB Output is correct
19 Correct 8 ms 23900 KB Output is correct
20 Correct 9 ms 23900 KB Output is correct
21 Correct 7 ms 23900 KB Output is correct
22 Correct 7 ms 24096 KB Output is correct
23 Correct 7 ms 23880 KB Output is correct
24 Correct 8 ms 23900 KB Output is correct
25 Correct 7 ms 23900 KB Output is correct
26 Correct 7 ms 23900 KB Output is correct
27 Correct 7 ms 23900 KB Output is correct
28 Correct 7 ms 23900 KB Output is correct
29 Correct 7 ms 24152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 23900 KB Output is correct
2 Correct 8 ms 23832 KB Output is correct
3 Correct 8 ms 23900 KB Output is correct
4 Correct 7 ms 23900 KB Output is correct
5 Correct 8 ms 23900 KB Output is correct
6 Correct 8 ms 23984 KB Output is correct
7 Correct 7 ms 23996 KB Output is correct
8 Correct 8 ms 23900 KB Output is correct
9 Correct 7 ms 23900 KB Output is correct
10 Correct 7 ms 24004 KB Output is correct
11 Correct 8 ms 23784 KB Output is correct
12 Correct 8 ms 23872 KB Output is correct
13 Correct 8 ms 23896 KB Output is correct
14 Correct 8 ms 23900 KB Output is correct
15 Correct 8 ms 23988 KB Output is correct
16 Correct 7 ms 23896 KB Output is correct
17 Correct 7 ms 23900 KB Output is correct
18 Correct 8 ms 23900 KB Output is correct
19 Correct 8 ms 23900 KB Output is correct
20 Correct 9 ms 23900 KB Output is correct
21 Correct 7 ms 23900 KB Output is correct
22 Correct 7 ms 24096 KB Output is correct
23 Correct 7 ms 23880 KB Output is correct
24 Correct 8 ms 23900 KB Output is correct
25 Correct 7 ms 23900 KB Output is correct
26 Correct 7 ms 23900 KB Output is correct
27 Correct 7 ms 23900 KB Output is correct
28 Correct 7 ms 23900 KB Output is correct
29 Correct 7 ms 24152 KB Output is correct
30 Correct 177 ms 61908 KB Output is correct
31 Correct 149 ms 60356 KB Output is correct
32 Correct 134 ms 125540 KB Output is correct
33 Correct 131 ms 103788 KB Output is correct
34 Correct 127 ms 103816 KB Output is correct
35 Correct 126 ms 93572 KB Output is correct
36 Correct 101 ms 74192 KB Output is correct
37 Correct 104 ms 60408 KB Output is correct
38 Correct 107 ms 58860 KB Output is correct
39 Correct 110 ms 58960 KB Output is correct
40 Correct 137 ms 58916 KB Output is correct
41 Correct 143 ms 59844 KB Output is correct
42 Correct 134 ms 59076 KB Output is correct
43 Correct 135 ms 106856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23900 KB Output is correct
2 Correct 7 ms 23900 KB Output is correct
3 Correct 7 ms 23900 KB Output is correct
4 Correct 8 ms 23900 KB Output is correct
5 Correct 7 ms 23980 KB Output is correct
6 Correct 8 ms 23900 KB Output is correct
7 Correct 8 ms 23948 KB Output is correct
8 Correct 7 ms 24016 KB Output is correct
9 Correct 7 ms 23964 KB Output is correct
10 Correct 7 ms 23900 KB Output is correct
11 Correct 8 ms 23900 KB Output is correct
12 Correct 8 ms 23900 KB Output is correct
13 Correct 7 ms 23932 KB Output is correct
14 Correct 8 ms 23900 KB Output is correct
15 Correct 8 ms 23932 KB Output is correct
16 Correct 8 ms 23896 KB Output is correct
17 Correct 8 ms 24088 KB Output is correct
18 Correct 8 ms 24060 KB Output is correct
19 Correct 7 ms 23896 KB Output is correct
20 Correct 8 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 23900 KB Output is correct
2 Correct 8 ms 23832 KB Output is correct
3 Correct 8 ms 23900 KB Output is correct
4 Correct 7 ms 23900 KB Output is correct
5 Correct 8 ms 23900 KB Output is correct
6 Correct 8 ms 23984 KB Output is correct
7 Correct 7 ms 23996 KB Output is correct
8 Correct 8 ms 23900 KB Output is correct
9 Correct 7 ms 23900 KB Output is correct
10 Correct 7 ms 24004 KB Output is correct
11 Correct 8 ms 23784 KB Output is correct
12 Correct 8 ms 23872 KB Output is correct
13 Correct 8 ms 23896 KB Output is correct
14 Correct 8 ms 23900 KB Output is correct
15 Correct 8 ms 23988 KB Output is correct
16 Correct 7 ms 23896 KB Output is correct
17 Correct 7 ms 23900 KB Output is correct
18 Correct 8 ms 23900 KB Output is correct
19 Correct 8 ms 23900 KB Output is correct
20 Correct 9 ms 23900 KB Output is correct
21 Correct 7 ms 23900 KB Output is correct
22 Correct 7 ms 24096 KB Output is correct
23 Correct 7 ms 23880 KB Output is correct
24 Correct 8 ms 23900 KB Output is correct
25 Correct 7 ms 23900 KB Output is correct
26 Correct 7 ms 23900 KB Output is correct
27 Correct 7 ms 23900 KB Output is correct
28 Correct 7 ms 23900 KB Output is correct
29 Correct 7 ms 24152 KB Output is correct
30 Correct 177 ms 61908 KB Output is correct
31 Correct 149 ms 60356 KB Output is correct
32 Correct 134 ms 125540 KB Output is correct
33 Correct 131 ms 103788 KB Output is correct
34 Correct 127 ms 103816 KB Output is correct
35 Correct 126 ms 93572 KB Output is correct
36 Correct 101 ms 74192 KB Output is correct
37 Correct 104 ms 60408 KB Output is correct
38 Correct 107 ms 58860 KB Output is correct
39 Correct 110 ms 58960 KB Output is correct
40 Correct 137 ms 58916 KB Output is correct
41 Correct 143 ms 59844 KB Output is correct
42 Correct 134 ms 59076 KB Output is correct
43 Correct 135 ms 106856 KB Output is correct
44 Correct 7 ms 23900 KB Output is correct
45 Correct 7 ms 23900 KB Output is correct
46 Correct 7 ms 23900 KB Output is correct
47 Correct 8 ms 23900 KB Output is correct
48 Correct 7 ms 23980 KB Output is correct
49 Correct 8 ms 23900 KB Output is correct
50 Correct 8 ms 23948 KB Output is correct
51 Correct 7 ms 24016 KB Output is correct
52 Correct 7 ms 23964 KB Output is correct
53 Correct 7 ms 23900 KB Output is correct
54 Correct 8 ms 23900 KB Output is correct
55 Correct 8 ms 23900 KB Output is correct
56 Correct 7 ms 23932 KB Output is correct
57 Correct 8 ms 23900 KB Output is correct
58 Correct 8 ms 23932 KB Output is correct
59 Correct 8 ms 23896 KB Output is correct
60 Correct 8 ms 24088 KB Output is correct
61 Correct 8 ms 24060 KB Output is correct
62 Correct 7 ms 23896 KB Output is correct
63 Correct 8 ms 23896 KB Output is correct
64 Correct 125 ms 106644 KB Output is correct
65 Correct 127 ms 112028 KB Output is correct
66 Correct 99 ms 60920 KB Output is correct
67 Correct 103 ms 60068 KB Output is correct
68 Correct 21 ms 29176 KB Output is correct
69 Correct 17 ms 28124 KB Output is correct
70 Correct 18 ms 29488 KB Output is correct
71 Correct 20 ms 31932 KB Output is correct
72 Correct 23 ms 32948 KB Output is correct
73 Correct 17 ms 27364 KB Output is correct
74 Correct 127 ms 103780 KB Output is correct
75 Correct 130 ms 103796 KB Output is correct
76 Correct 133 ms 106364 KB Output is correct
77 Correct 137 ms 107012 KB Output is correct
78 Correct 128 ms 99700 KB Output is correct
79 Correct 126 ms 99728 KB Output is correct
80 Correct 132 ms 125448 KB Output is correct
81 Correct 238 ms 121472 KB Output is correct
82 Correct 238 ms 121472 KB Output is correct
83 Correct 111 ms 84660 KB Output is correct
84 Correct 113 ms 79272 KB Output is correct
85 Correct 95 ms 67808 KB Output is correct
86 Correct 93 ms 62200 KB Output is correct
87 Correct 189 ms 129112 KB Output is correct
88 Correct 161 ms 113236 KB Output is correct
89 Correct 120 ms 91284 KB Output is correct
90 Correct 99 ms 60164 KB Output is correct
91 Correct 101 ms 59384 KB Output is correct
92 Correct 111 ms 58964 KB Output is correct