Submission #624650

# Submission time Handle Problem Language Result Execution time Memory
624650 2022-08-08T15:02:32 Z wiwiho Ancient Books (IOI17_books) C++14
0 / 100
1 ms 340 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) return;
    dsu[b] = a;
    mx[a] = max(mx[a], mx[b]);
}

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

    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<vector<int>> child(n + 1);
    vector<int> pr(n + 1, -1);

    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;
            }
        }

        if(mx[p] == i){
            st.pob;
            if(st.empty()) child[n].eb(p), pr[p] = n;
            else child[st.back()].eb(p), pr[p] = st.back();
        }
    }

    int now = s;
    while(pr[now] != -1){
        int p = pr[now];
        int l = 1, r = 1;
        int pos = -1;
        for(int i : child[p]){
            if(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;
    }

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 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 Incorrect 1 ms 296 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 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 Incorrect 1 ms 296 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 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 Incorrect 1 ms 296 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 220 KB Output is correct
3 Correct 1 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 Incorrect 1 ms 296 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -