Submission #1061468

# Submission time Handle Problem Language Result Execution time Memory
1061468 2024-08-16T09:32:03 Z Zicrus Ancient Books (IOI17_books) C++17
0 / 100
3 ms 1364 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++) {
        for (int b = a+1; b < cyc.size(); b++) {
            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]));
                }
            }
            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:43: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]
   43 |         for (int b = a+1; b < cyc.size(); b++) {
      |                           ~~^~~~~~~~~~~~
books.cpp:45:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |             for (int i = 0; i < cyc[a].size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~
books.cpp:46:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |                 for (int j = 0; j < cyc[b].size(); j++) {
      |                                 ~~^~~~~~~~~~~~~~~
books.cpp:55: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]
   55 |     for (int i = 0; i < cyc.size(); i++) lnk[i] = i;
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1364 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4074'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -