이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<vector<int>> adj;
vector<int> dist;
long long minimum_walk(vector<int> p, int s) {
ll tot = 0;
int n = p.size();
vector<bool> taken(n, false);
int g = n;
adj.resize(n);
for(int i = 1; i < n; i++) {
adj[i].push_back(i-1);
adj[i-1].push_back(i);
}
for(int i = 0; i < n; i++) {
if(!taken[i]) {
adj.push_back(vector<int>());
int x = i;
int l = i, r = i;
while(!taken[x]) {
taken[x] = true;
tot += abs(x - p[x]);
x = p[x];
l = min(l, x);
r = max(r, x);
adj[x].push_back(g);
}
for(int j = l; j <= r; j++) {
adj[g].push_back(j);
}
g++;
}
}
taken.clear();
taken.resize(g, false);
dist.resize(g, 1e9 + 7);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s + 1});
while(pq.size()) {
auto pa = pq.top();
pq.pop();
int len = pa.first, x = abs(pa.second) - 1;
if(taken[x]) continue;
taken[x] = true;
if(x < n) {
if(pa.second > 0) {
tot += 2;
}
}
for(int ch : adj[x]) {
int goLength;
if(max(ch, x) < n) {
goLength = len + 1;
}
else {
goLength = len;
}
if(dist[ch] < goLength) continue;
if(dist[ch] == goLength && len != goLength) continue;
dist[ch] = goLength;
if(goLength == len + 1) pq.push({goLength, ch+1});
else pq.push({goLength, -ch-1});
}
}
for(int i = 0; i < n; i++) {
if(p[i] == i && i != s) tot -= 2;
else break;
}
for(int i = n-1; i >= 0; i--) {
if(p[i] == i && i != s) tot -= 2;
else break;
}
return max(tot - 2, 0LL);
}
# | 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... |