#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pii> p3i;
LL minimum_walk(vector<int> p, int s)
{
LL ans = 0LL;
int N = p.size();
for(int i = 0; i < N; i++)
ans += abs(i - p[i]);
vector<int> f;
f.resize(N);
for(int i = 0; i < N; i++) f[i] = i;
function<int(int)> F = [&](int x) -> int { if(x == f[x]) return x; return f[x] = F(f[x]); } ;
for(int i = 0; i < N; i++)
{
int fx = F(i), fy = F(p[i]);
if(fx == fy) continue;
f[fy] = fx;
}
vector<int> nodes;
vector<p3i> edges;
for(int i = 0; i < N; i++)
if(p[i] != i)
nodes.push_back(i);
for(int i = 0; i < nodes.size(); i++)
{
int nod = nodes[i];
edges.push_back({abs(s - nod), {s, nod}});
if(i > 0)
{
int lst = nodes[i - 1];
edges.push_back({abs(nod - lst), {nod, lst}});
}
}
sort(begin(edges), end(edges));
for(auto &e: edges)
{
int cst = e.first, x = e.second.first, y = e.second.second;
int fx = F(x), fy = F(y);
if(fx == fy) continue;
ans += 2LL * cst;
f[fy] = fx;
}
return ans;
}
Compilation message
books.cpp: In function 'LL minimum_walk(std::vector<int>, int)':
books.cpp:34:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < nodes.size(); i++)
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Incorrect |
3 ms |
372 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 |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Incorrect |
3 ms |
372 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 |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Incorrect |
3 ms |
372 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 |
384 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 |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Incorrect |
3 ms |
372 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |