이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifndef LOCAL
#include "books.h"
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
int n;
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
int cur = 0;
ll res = 0;
vector<int> vis(n , -1);
vector<int> mx(n , -1);
for(int i = 0 ; i < n ; ++i){
if(vis[i] != -1 || p[i] == i)continue;
int j = i;
while(true){
vis[j] = i;
mx[i] = max(mx[i] , j);
if(p[j] == i)break;
j = p[j];
}
}
int tmp = 0;
for(int i = 0 ; i < n ; ++i){
if(vis[i] == i){
if(tmp < i)res += 2 * (i - tmp);
tmp = max(tmp , mx[i]);
}
res += abs(i - p[i]);
}
return res;
}
#ifdef LOCAL
#include "books.h"
#include <cstdio>
#include <vector>
#include <cassert>
using namespace std;
int main() {
freopen(taskname".INP","r",stdin);
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
#endif // LOCAL
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:28:9: warning: unused variable 'cur' [-Wunused-variable]
28 | int cur = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |