답안 #1061476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1061476 2024-08-16T09:38:53 Z Zicrus 고대 책들 (IOI17_books) C++17
12 / 100
3 ms 1368 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

typedef long long ll;

vector<ll> lnk;

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

void unite(ll a, ll b) {
    a = find(a); b = find(b);
    lnk[a] = b;
}

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

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

    priority_queue<pair<ll, pair<ll, ll>>> q;
    for (int a = 0; a < cyc.size(); a++) {
        ll mnA = n, mxA = 0;
        for (auto &e : cyc[a]) { mnA = min(mnA, e); mxA = max(mxA, e); }
        for (int b = a+1; b < cyc.size(); b++) {
            ll mnB = n, mxB = 0;
            for (auto &e : cyc[b]) { mnB = min(mnB, e); mxB = max(mxB, e); }
            ll mn = n;
            for (int i = 0; i < cyc[a].size(); i++) {
                for (int j = 0; j < cyc[b].size(); j++) {
                    mn = min(mn, abs(cyc[a][i] - cyc[b][j]));
                    if (cyc[a][i] > mnB && cyc[a][i] < mxB) mn = 0;
                    if (cyc[b][i] > mnA && cyc[b][i] < mxA) mn = 0;
                }
            }
            q.push({-mn, {a, b}});
        }
    }

    lnk = vector<ll>(cyc.size());
    for (int i = 0; i < cyc.size(); i++) lnk[i] = i;
    while (!q.empty()) {
        ll cost = -q.top().first;
        pair<ll, ll> p = q.top().second;
        q.pop();
        if (find(p.first) != find(p.second)) {
            unite(p.first, p.second);
            res += 2*cost;
        }
    }

    return res;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int a = 0; a < cyc.size(); a++) {
      |                     ~~^~~~~~~~~~~~
books.cpp:45:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         for (int b = a+1; b < cyc.size(); b++) {
      |                           ~~^~~~~~~~~~~~
books.cpp:49:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |             for (int i = 0; i < cyc[a].size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~
books.cpp:50:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |                 for (int j = 0; j < cyc[b].size(); j++) {
      |                                 ~~^~~~~~~~~~~~~~~
books.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < cyc.size(); i++) lnk[i] = i;
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Incorrect 3 ms 1368 KB 3rd lines differ - on the 1st token, expected: '2082', found: '2062'
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Incorrect 3 ms 1368 KB 3rd lines differ - on the 1st token, expected: '2082', found: '2062'
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1364 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Incorrect 3 ms 1368 KB 3rd lines differ - on the 1st token, expected: '2082', found: '2062'
23 Halted 0 ms 0 KB -