Submission #271509

# Submission time Handle Problem Language Result Execution time Memory
271509 2020-08-18T06:29:19 Z hamerin Ancient Books (IOI17_books) C++17
0 / 100
1 ms 512 KB
#include "books.h"

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

using i64 = long long;
using d64 = long double;
using pi = pair<int, int>;
using pli = pair<i64, i64>;
using ti = tuple<int, int, int>;
using tli = tuple<i64, i64, i64>;

#define iterall(cont) cont.begin(), cont.end()
#define prec(n) setprecision(n) << fixed

i64 minimum_walk(vector<int> p, int s) {
    const int N = p.size();

    vector<vector<int>> cycles;
    vector<set<int>> cyc_st;
    vector<int> cyc(N);

    vector<bool> vst(N);
    for (int i = 0; i < N; i++) {
        if (vst[i]) continue;

        cycles.emplace_back(vector<int>());
        cyc_st.emplace_back(set<int>());

        int cur = i;
        while (cycles.back().empty() || cur != i) {
            cycles.back().emplace_back(cur);
            cyc_st.back().emplace(cur);
            cyc[cur] = cycles.size() - 1;
            vst[cur] = true;
            cur = p[cur];
        }
    }

    i64 ans = 0;
    for (int i = 0; i < N; i++) ans += abs(i - p[i]);

    vector<pi> intervals;
    for (auto &st : cyc_st)
        intervals.emplace_back(*st.begin(), *prev(st.end()));
    sort(iterall(intervals));

    // interval 합치기
    vector<pi> newIntervals;
    for (auto [l, r] : intervals) {
        if (!newIntervals.empty() && newIntervals.back().second > l) {
            newIntervals.back().second = r;
        } else {
            newIntervals.emplace_back(l, r);
        }
    }

    while (!newIntervals.empty() &&
           newIntervals.back().first == newIntervals.back().second)
        newIntervals.pop_back();

    for (int i = 0; i < newIntervals.size() - 1; i++) {
        ans += 2 * (newIntervals[i + 1].first - newIntervals[i].second);
    }

    // Strategy
    // Disjoint Set의 Lmost, Rmost interval까지의 거리 저장
    // Recursive하게 dp로 해결 가능할 듯


    return ans;
}

Compilation message

books.cpp: In function 'i64 minimum_walk(std::vector<int>, int)':
books.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (int i = 0; i < newIntervals.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Runtime error 1 ms 384 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Runtime error 1 ms 384 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Runtime error 1 ms 384 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 512 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4168'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Runtime error 1 ms 384 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -