제출 #125064

#제출 시각아이디문제언어결과실행 시간메모리
125064nvmdava고대 책들 (IOI17_books)C++17
100 / 100
165 ms8440 KiB
#include <bits/stdc++.h> using namespace std; long long res; inline void push(int& l, int& r, int& mn, int& mx, vector<int>& p){ while(l != mn || r != mx){ if(l != mn){ l--; mn = min(mn, p[l]); mx = max(mx, p[l]); } else { r++; mn = min(mn, p[r]); mx = max(mx, p[r]); } } } long long minimum_walk(vector<int> p, int s){ int L = 0, R = p.size() - 1; while(R >= 0 && p[R] == R) R--; if(R == -1) return 0; while(p[L] == L) L++; if(s > R){ res += (s - R) << 1; s = R; } if(s < L){ res += (L - s) << 1; s = L; } int tmn = s, tmx = s; for(int i = L; i <= R; i++){ res += abs(p[i] - i); if(1LL * (tmx - p[i]) * (tmx - i) <= 0 || 1LL * (tmn - p[i]) * (tmn - i) <= 0){ tmx = max(tmx, max(i, p[i])); tmn = min(tmn, min(i, p[i])); } } int l, r, mn = min(s, p[s]), mx = max(s, p[s]); l = r = s; push(l, r, tmn, tmx, p); l = r = s; push(l, r, mn, mx, p); while(mn != tmn || mx != tmx){ res += 2; if(mn != tmn)mn--; if(mx != tmx)mx++; push(l, r, mn, mx, p); } for(; l >= L; l--){ if(mn > l) res += 2; mn = min(mn, p[l]); } for(; r <= R; r++){ if(mx < r) res += 2; mx = max(mx, p[r]); } return res; }
#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...