답안 #1061643

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1061643 2024-08-16T11:24:28 Z Zicrus 고대 책들 (IOI17_books) C++17
0 / 100
1 ms 604 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

typedef long long ll;

vector<ll> lnk;
vector<ll> mnUF, mxUF;

ll find(ll a) {
    if (lnk[a] != a) lnk[a] = find(lnk[a]);
    return lnk[a];
}

bool same(ll a, ll b) {
    return find(a) == find(b);
}

void unite(ll a, ll b) {
    a = find(a); b = find(b);
    if (a == b) return;
    lnk[a] = b;
    mnUF[b] = min(mnUF[b], mnUF[a]);
    mxUF[b] = max(mxUF[b], mxUF[a]);
}

ll minimum_walk(vector<int> p, int s) {
    ll n = p.size();
	ll res = 0;
    for (int i = 0; i < n; i++) res += abs(p[i] - i);
    if (res == 0) return 0;

    vector<bool> vst(n);
    vector<vector<ll>> cyc;
    vector<ll> mxCyc;
    vector<ll> cycId(n);
    for (int i = 0; i < n; i++) {
        if (vst[i] || p[i] == i) continue;
        cyc.push_back(vector<ll>());
        mxCyc.push_back(i);
 
        ll cur = i;
        do {
            cyc.back().push_back(cur);
            mxCyc.back() = max(mxCyc.back(), cur);
            cycId[cur] = cyc.size()-1;
            cur = p[cur];
            vst[cur] = true;
        } while (cur != i);
    }
    ll m = cyc.size();

    vector<pair<ll, ll>> cycMM(m);
    ll allMn = n, allMx = 0;
    for (int i = 0; i < m; i++) {
        cycMM[i] = {cyc[i][0], mxCyc[i]};
        allMn = min(allMn, cycMM[i].first);
        allMx = max(allMx, cycMM[i].second);
    }
    vector<ll> lnk(m), mnUF(m), mxUF(m);
    for (int i = 0; i < m; i++) {
        lnk[i] = i;
        mnUF[i] = cyc[i][0];
        mxUF[i] = mxCyc[i];
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            if (cyc[i][0] < cyc[j][0] && mxCyc[i] > cyc[j][0] && mxCyc[i] < mxCyc[j]) {
                unite(i, j);
            }
        }
    }
    for (int i = 0; i < m; i++) {
        mnUF[i] = mnUF[find(i)];
        mxUF[i] = mxUF[find(i)];
    }

    ll left = s, right = s;
    ll mn = left, mx = right;
    if (p[left] != left) {
        mn = min(mnUF[cycId[left]], mn);
        mx = max(mxUF[cycId[left]], mx);
    }
    if (p[right] != right) {
        mn = min(mnUF[cycId[right]], mn);
        mx = max(mxUF[cycId[right]], mx);
    }
    left = mn; right = mx;
    while (left > allMn || right < allMx) {
        left = max(0ll, left-1);
        right = min(n-1, right+1);
        res += 2;
        ll mn = left, mx = right;
        if (p[left] != left) {
            mn = min(mnUF[cycId[left]], mn);
            mx = max(mxUF[cycId[left]], mx);
        }
        if (p[right] != right) {
            mn = min(mnUF[cycId[right]], mn);
            mx = max(mxUF[cycId[right]], mx);
        }
        left = mn; right = mx;
    }
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -