답안 #1061512

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

typedef long long ll;

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);
    for (int i = 0; i < m; i++) cycMM[i] = {cyc[i][0], mxCyc[i]};
    for (int i = m-1; i >= 0; i--) {
        for (int j = i+1; j < m && cycMM[j].first < cycMM[i].second; j++) {
            if (cycMM[j].second > cycMM[i].second) {
                cycMM[i].first = cycMM[j].first = min(cycMM[i].first, cycMM[j].first);
                cycMM[i].second = cycMM[j].second = max(cycMM[i].second, cycMM[j].second);
            }
        }
    }

    ll left = s, right = s;
    while (left > 0 || right < n-1) {
        ll mn = max(0ll, left-1);
        ll mx = min(n-1, right+1);
        res += 2;
        if (p[mn] != mn) {
            mn = min(cycMM[cycId[mn]].first, mn);
            mx = max(cycMM[cycId[mn]].second, mx);
        }
        if (p[mx] != mx) {
            mn = min(cycMM[cycId[mx]].first, mn);
            mx = max(cycMM[cycId[mx]].second, mx);
        }
        left = mn; right = mx;
    }
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3306'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -