#include <bits/stdc++.h>
using namespace std;
const int c=100002;
bool v[c];
long long sum;
vector<pair<int, int> > sz;
int n, t[c], mini, maxi;
void dfs(int a) {
v[a]=true;
mini=min(mini, a), maxi=max(maxi, a);
if (!v[t[a]]) dfs(t[a]);
}
long long minimum_walk(vector<int> p, int s) {
n=p.size();
for (int i=0; i<n; i++) t[i]=p[i], sum+=(abs(p[i]-i));
for (int i=0; i<n; i++) if (!v[i] && t[i]!=i) mini=n, maxi=0, dfs(i), sz.push_back({mini, maxi});
maxi=0;
for (int i=1; i<sz.size(); i++) {
maxi=max(maxi, sz[i-1].second);
sum+=2*max(0, sz[i].first-maxi);
}
return sum;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:19:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i=1; i<sz.size(); i++) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |