제출 #137650

#제출 시각아이디문제언어결과실행 시간메모리
137650MAMBA고대 책들 (IOI17_books)C++17
0 / 100
2 ms376 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define rep(i , j , k) for(int i = j; i < (int)k; i++) typedef long long ll; const int N = 1e6 + 10; int par[N]; ll le[N], ri[N], arr[N]; int getPar(int v) { return v == par[v] ? v : par[v] = getPar(par[v]); } inline bool merge(int u , int v) { if ((u = getPar(u)) == (v = getPar(v))) return false; par[v] = u; return true; } inline void smax(ll &a, ll b) { if (a < b) a = b; } inline void smin(ll &a, ll b) { if (b < a) a = b; } long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); iota(par , par + n , 0); ll res = 0; rep(i , 0 , n) { res += abs(p[i] - i); merge(p[i] , i); } fill(le , le + n , -1e9); fill(ri , ri + n , 1e9); rep(i , 0 , n) { if (i < s) smax(le[getPar(i)] , i); else if (i > s) smin(ri[getPar(i)] , i); } ll Rlimit = s , Llimit = s; fill(arr ,arr + n , s); rep(i , 0 , n) { if (par[i] == i && i != getPar(s) && i != p[i]) { if (le[i] < 0) smax(Rlimit , ri[i]); if (ri[i] >= n) smin(Llimit , le[i]); if (le[i] >= 0) arr[le[i]] = ri[i]; } } ll ans = 1e18; rep(i , 0 , s + 1) { if (i <= Llimit) smin(ans , res + (Rlimit - i) * 2); smax(Rlimit , arr[i]); } return ans; }
#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...