Submission #790526

#TimeUsernameProblemLanguageResultExecution timeMemory
790526skittles1412Ancient Books (IOI17_books)C++17
42 / 100
2064 ms18664 KiB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using ll = long long;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

ll minimum_walk(vector<int> arr, int kv) {
    int n = sz(arr), trans[n - 1] {};
    for (int i = 0; i < n; i++) {
        for (int j = i; j < arr[i]; j++) {
            trans[j]++;
            dbg(i, j);
        }
    }
    int sort_p, sort_s;
    for (sort_p = 0; sort_p < n && arr[sort_p] == sort_p; sort_p++)
        ;
    for (sort_s = n - 1; sort_s >= 0 && arr[sort_s] == sort_s; sort_s--)
        ;
    int dp[n][n];
    memset(dp, 0x3f, sizeof(dp));
    dp[0][n - 1] = 0;
    for (int l = 0; l < n; l++) {
        for (int r = n - 1; r >= 0; r--) {
            if (l <= sort_p && r >= sort_s) {
                dp[l][r] = 0;
            }
            for (int i = l; i <= r; i++) {
                dp[l][r] = min({dp[l][r], dp[arr[i]][r], dp[l][arr[i]]});
            }
            if (l) {
                dp[l][r] = min(dp[l][r], 1 + dp[l - 1][r]);
            }
            if (r + 1 < n) {
                dp[l][r] = min(dp[l][r], 1 + dp[l][r + 1]);
            }
        }
    }
    long ans = 2 * dp[kv][kv];
    for (auto& a : trans) {
        ans += a * 2;
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...