답안 #624667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624667 2022-08-08T15:09:42 Z wiwiho 고대 책들 (IOI17_books) C++14
0 / 100
1 ms 468 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<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;
            }
        }

        if(mx[p] == i){
            st.pob;
            if(st.empty()) root.eb(p), pr[p] = -1;
            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 = 0; i < (int)child[p].size(); i++){
            if(child[p][i] == now) pos = i;
        }
        assert(pos != -1);
        
        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;

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Runtime error 1 ms 340 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Runtime error 1 ms 340 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Runtime error 1 ms 340 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Runtime error 1 ms 340 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -