Submission #624683

# Submission time Handle Problem Language Result Execution time Memory
624683 2022-08-08T15:25:12 Z wiwiho Ancient Books (IOI17_books) C++14
50 / 100
210 ms 58196 KB
#include "books.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

int n;
vector<int> dsu, mx;

int findDSU(int a){
    if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
    return dsu[a];
}

void unionDSU(int a, int b){
    a = findDSU(a);
    b = findDSU(b);
    if(a > b) swap(a, b);
    if(a == b) return;
    dsu[b] = a;
    mx[a] = max(mx[a], mx[b]);
}

ll minimum_walk(vector<int> arr, int s){
    n = arr.size();
    //printv(arr, cerr);

    dsu.resize(n);
    mx.resize(n);
    iota(iter(dsu), 0);
    iota(iter(mx), 0);

    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += abs(i - arr[i]);
        unionDSU(i, arr[i]);
    }

    vector<int> st;
    vector<int> vst(n);
    vector<int> root;
    vector<vector<int>> child(n);
    vector<int> pr(n);

    for(int i = 0; i < n; i++){
        int p = findDSU(i);
        if(vst[p] == 0){
            vst[p] = 1;
            st.eb(p);
        }
        else{
            while(st.back() != p){
                unionDSU(p, st.back());
                st.pob;
            }
        }
        //cerr << "owo " << i << " " << p << " : ";
        //printv(st, cerr);

        if(mx[p] == i){
            st.pob;
            if(st.empty()) pr[p] = -1;
            else pr[p] = st.back();
        }
    }

    for(int i = 0; i < n; i++){
        if(dsu[i] != i) continue;
        if(pr[i] == -1){
            root.eb(i);
            continue;
        }
        pr[i] = findDSU(pr[i]);
        child[pr[i]].eb(i);
    }

    int now = s;
    while(pr[now] != -1){
        int p = pr[now];
        //cerr << "test " << now << " " << p << "\n";
        //printv(child[p], cerr);
        int l = 1, r = 1;
        int pos = -1;
        for(int i = 0; i < (int)child[p].size(); i++){
            if(child[p][i] == now) pos = i;
        }
        
        int lst = now;
        for(int i = pos - 1; i >= 0; i--){
            int c = child[p][i];
            if(mx[c] + 1 != lst) break;
            l++;
            lst = c;
        }

        lst = mx[now];
        for(int i = pos + 1; i < (int)child[p].size(); i++){
            int c = child[p][i];
            if(lst + 1 != c) break;
            r++;
            lst = mx[c];
        }

        ans += min(l, r) * 2;
        now = p;
    }

    ans += ((int)root.size() - 1) * 2;
    for(int i = 0; i < now; i++){
        if(arr[i] != i) break;
        ans -= 2;
    }
    for(int i = n - 1; i > mx[now]; i--){
        if(arr[i] != i) break;
        ans -= 2;
    }

	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 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 284 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 392 KB Output is correct
12 Correct 1 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 0 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 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 284 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 392 KB Output is correct
12 Correct 1 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 0 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 340 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 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 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 296 KB Output is correct
29 Correct 1 ms 340 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 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 284 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 392 KB Output is correct
12 Correct 1 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 0 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 340 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 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 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 296 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 181 ms 49084 KB Output is correct
31 Correct 210 ms 49188 KB Output is correct
32 Correct 129 ms 53376 KB Output is correct
33 Correct 160 ms 54172 KB Output is correct
34 Correct 169 ms 54240 KB Output is correct
35 Correct 148 ms 52664 KB Output is correct
36 Correct 165 ms 51536 KB Output is correct
37 Correct 139 ms 49224 KB Output is correct
38 Correct 140 ms 49156 KB Output is correct
39 Correct 167 ms 49088 KB Output is correct
40 Correct 146 ms 49148 KB Output is correct
41 Correct 172 ms 49160 KB Output is correct
42 Correct 189 ms 49112 KB Output is correct
43 Correct 168 ms 58196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2746'
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 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 284 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 392 KB Output is correct
12 Correct 1 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 0 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 340 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 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 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 296 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 181 ms 49084 KB Output is correct
31 Correct 210 ms 49188 KB Output is correct
32 Correct 129 ms 53376 KB Output is correct
33 Correct 160 ms 54172 KB Output is correct
34 Correct 169 ms 54240 KB Output is correct
35 Correct 148 ms 52664 KB Output is correct
36 Correct 165 ms 51536 KB Output is correct
37 Correct 139 ms 49224 KB Output is correct
38 Correct 140 ms 49156 KB Output is correct
39 Correct 167 ms 49088 KB Output is correct
40 Correct 146 ms 49148 KB Output is correct
41 Correct 172 ms 49160 KB Output is correct
42 Correct 189 ms 49112 KB Output is correct
43 Correct 168 ms 58196 KB Output is correct
44 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2746'
45 Halted 0 ms 0 KB -