Submission #1061459

#TimeUsernameProblemLanguageResultExecution timeMemory
1061459ZicrusAncient Books (IOI17_books)C++17
0 / 100
3 ms1112 KiB
#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 (stderr)

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 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...