이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inside sl<=l&&r<=sr
#define outside r<sl||sr<l
#define inf 1000000009
using namespace std;
typedef long long ll;
int n;
vector<int> arr;
vector<ll> numara;
int num = 0;
vector<pair<ll, ll> > minler;
ll minimum_walk(vector<int> p, int s){
arr = p;
n = p.size();
numara.resize(n);
ll ans = 0;
for (int i = 0; i < n; i++) numara[i] = -1;
for (int i = 0; i < n; i++){
if (numara[i] == -1 && p[i] != i){
int node = i;
int lmost = node;
int rmost = node;
while (numara[node] == -1){
numara[node] = num;
node = p[node];
lmost = min(lmost, node);
rmost = max(rmost, node);
}
num++;
ans += (2*(rmost-lmost));
}
}
//for (int i = 0; i < n; i++) cout << numara[i] << " "; cout << endl;
//cout << "num: " << num << endl;
minler.resize(num);
for (int i = 0; i < num; i++) minler[i] = mp(inf, inf);
for (int i = s; i < n; i++){
if (numara[i] != -1 && minler[numara[i]].second == inf){
minler[numara[i]].second = i-s;
}
}
for (int i = s; i >= 0; i--){
if (numara[i] != -1 && minler[numara[i]].first == inf){
minler[numara[i]].first = s-i;
}
}
minler.pb(mp(0, inf));
minler.pb(mp(inf, 0));
sort(minler.begin(), minler.end());
ll best = inf;
ll cmax = 0;
for (int i = minler.size()-1; i > 0; i--){
cmax = max(cmax, minler[i].second);
best = min(best, cmax + minler[i-1].first);
}
//cout << "ans: " << ans <<endl;
ans += best*2;
return ans;
}
# | 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... |