이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "books.h"
//#include "Lgrader.cpp"
using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
int n;
int p[MAXN];
bool used[MAXN];
pair<int, int> dfs(int u)
{
used[u] = true;
pair<int, int> ret = {u, u};
if(!used[p[u]])
{
pair<int, int> oth = dfs(p[u]);
chkmax(ret.second, oth.second);
chkmin(ret.first, oth.first);
}
return ret;
}
/*
int L, R;
int mn[1 << 10][1 << 10];
int mx[1 << 10][1 << 10];
int dp[1 << 10][1 << 10];
int Min(int l, int r)
{
if(l == r) return p[l];
int &memo = mn[l][r];
if(memo != -1) return memo;
return memo = min(Min(l + 1, r), Min(l, r - 1));
}
int Max(int l, int r)
{
if(l == r) return p[l];
int &memo = mx[l][r];
if(memo != -1) return memo;
return memo = max(Max(l + 1, r), Max(l, r - 1));
}
int rec(int l, int r)
{
if(l == L && r == R)
return 0;
int &memo = dp[l][r];
if(memo != -1) return memo;
int nl = Min(l, r);
if(nl < l) return memo = rec(nl, r);
int nr = Max(l, r);
if(r < nr) return memo = rec(l, nr);
memo = (int)1e9;
if(L < l) chkmin(memo, rec(l - 1, r) + 2);
if(r < R) chkmin(memo, rec(l, r + 1) + 2);
return memo;
}*/
int mn[MAXN];
int mx[MAXN];
int lg2[MAXN];
int dist[MAXN];
bool in_queue[MAXN];
long long minimum_walk(std::vector<int> p, int s)
{
n = p.size();
for(int i = 0; i < n; i++)
::p[i] = p[i];
int64_t answer = 0;
for(int i = 0; i < n; i++)
answer += abs(i - p[i]);
vector<pair<int, int> > li;
for(int i = 0; i < n; i++)
if(i != p[i] && !used[i])
li.push_back(dfs(i));
li.push_back({s, s});
sort(li.begin(), li.end());
vector<pair<int, int> > tmp;
for(int i = 0; i < (int)li.size(); i++)
if(tmp.empty() || tmp.back().second < li[i].first) tmp.push_back(li[i]);
else chkmax(tmp[tmp.size() - 1].second, li[i].second);
li = tmp;
int last = li[0].first;
for(auto it: li)
{
answer += 2 * (it.first - last);
last = it.second;
}
if(s != 0)
{
int l, r;
for(auto it: li)
if(it.first <= s && s <= it.second)
l = it.first, r = it.second;
//memset(mn, -1, sizeof(mn));
//memset(mx, -1, sizeof(mx));
//memset(dp, -1, sizeof(dp));
//answer += rec(s, s);
mn[s] = mx[s] = p[s];
for(int i = s + 1; i <= r; i++) mn[i] = min(mn[i - 1], p[i]);
for(int i = s + 1; i <= r; i++) mx[i] = max(mx[i - 1], p[i]);
for(int i = s - 1; i >= l; i--) mn[i] = min(mn[i + 1], p[i]);
for(int i = s - 1; i >= l; i--) mx[i] = max(mx[i + 1], p[i]);
for(int i = l; i <= r; i++) dist[i] = (int)1e9;
deque<int> Q;
Q.push_back(s);
dist[s] = 0;
in_queue[s] = 1;
while(!Q.empty())
{
int u = Q.front();
Q.pop_front();
in_queue[u] = 0;
if(chkmin(dist[mn[u]], dist[u]))
{
if(!in_queue[mn[u]]) Q.push_front(mn[u]);
in_queue[mn[u]] = 1;
}
if(chkmin(dist[mx[u]], dist[u]))
{
if(!in_queue[mx[u]]) Q.push_front(mx[u]);
in_queue[mx[u]] = 1;
}
if(u < r && chkmin(dist[u + 1], 2 + dist[u]))
{
if(!in_queue[u + 1]) Q.push_back(u + 1);
in_queue[u + 1] = 1;
}
if(u > l && chkmin(dist[u - 1], 2 + dist[u]))
{
if(!in_queue[u - 1]) Q.push_back(u - 1);
in_queue[u - 1] = 1;
}
}
answer += min(dist[l], dist[r]);
}
return answer;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:113:10: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
int l, r;
^
books.cpp:113:7: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
int l, r;
^
# | 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... |