#include "books.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
using namespace std;
struct unionfind
{
vector<int> root = vector<int>(1005);
int find(int i)
{
if (root[i] == i)
return i;
return root[i] = find(root[i]);
}
bool unite(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return false;
root[a] = b;
return true;
}
} un;
struct qnode
{
int dis, a, b;
};
long long
minimum_walk(std::vector<int> p, int s)
{
iota(un.root.begin(), un.root.end(), 0);
vector<int> cycle(p.size(), -1);
vector<vector<int>> cycles;
long long triplenght = 0;
for (int i = 0; i < p.size(); i++)
{
if (cycle[i] == -1 && p[i] != i)
{
int t = i;
cycles.push_back({});
do
{
cycles[cycles.size() - 1].push_back(t);
cycle[t] = cycles.size();
triplenght += abs(t - p[t]);
t = p[t];
} while (t != i);
}
}
vector<qnode> edges;
for (int i = 0; i < cycles.size(); i++)
{
for (int e = 0; e < i; e++)
{
int mn = INT32_MAX;
for (int a : cycles[i])
for (int b : cycles[e])
mn = min(mn, abs(a - b));
edges.push_back({mn, i, e});
}
}
sort(edges.begin(), edges.end(), [](qnode a, qnode b)
{ return a.dis < b.dis; });
for (qnode n : edges)
{
if (un.unite(n.a, n.b))
triplenght += 2 * n.dis;
}
int t = 0;
while (cycles.size() && cycle[t++] == -1)
triplenght += 2;
return triplenght;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < p.size(); i++)
| ~~^~~~~~~~~~
books.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < cycles.size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
296 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
296 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
296 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
784 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
296 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |