제출 #968244

#제출 시각아이디문제언어결과실행 시간메모리
968244nguyentunglam고대 책들 (IOI17_books)C++17
0 / 100
1 ms348 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); vector<int> vis(n); int last = 0; long long ans = 0; for(int i = 0; i < n; i++) if (!vis[i]) { last = i; int cur = i; vis[cur] = 0; int pre = cur; cur = p[cur]; while (!vis[cur]) { vis[cur] = 1; ans += abs(pre - cur); pre = cur; cur = p[cur]; } } ans += last * 2; return ans; } #ifdef ngu int main() { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); 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 // ngu
#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...