이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<bool> VB;
typedef vector<VB> VVB;
void solve() {
int n;
cin >> n;
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
r1--, c1--, r2--, c2--;
VI a(n);
for (int& x : a) cin >> x;
VI ls, rs;
ls = rs = VI(n);
if (true) {
stack<int> st;
st = stack<int>();
replr(i, 0, n-1) {
while (st.size() && a[st.top()] >= a[i]) st.pop();
if (st.size()) ls[i] = st.top();
else ls[i] = -1;
st.push(i);
}
st = stack<int>();
reprl(i, n-1, 0) {
while (st.size() && a[st.top()] >= a[i]) st.pop();
if (st.size()) rs[i] = st.top();
else rs[i] = n;
st.push(i);
}
}
VVPI gp(2*n+2);
/*
* [0, n-1] - leftmost cells
* [n, 2n-1] - rightmost cells
* 2n - starting cell
* 2n+1 - ending cell
*/
rep(u, n) {
if (u) gp[u].pb({u-1+n, 1});
if (u) gp[u].pb({u-1, 1});
if (u != n-1) gp[u].pb({u+1, 1});
}
rep(u, n) {
if (u != n-1) gp[u+n].pb({u+1, 1});
if (ls[u] != -1) gp[u+n].pb({ls[u]+n, u-ls[u]});
if (rs[u] != n) gp[u+n].pb({rs[u]+n, rs[u]-u});
}
replr(r, r1, n-1) {
if (a[r] < c1) {
gp[2*n].pb({r+n, abs(r1-r)});
break;
}
gp[2*n].pb({r, abs(r1-r) + (c1-0)});
gp[2*n].pb({r+n, abs(r1-r) + (a[r]-c1)});
}
reprl(r, r1, 0) {
if (a[r] < c1) {
gp[2*n].pb({r+n, abs(r1-r)});
break;
}
gp[2*n].pb({r, abs(r1-r) + (c1-0)});
gp[2*n].pb({r+n, abs(r1-r) + (a[r]-c1)});
}
replr(r, r2, n-1) {
gp[r].pb({2*n+1, abs(r2-r) + (c2-0)});
gp[r+n].pb({2*n+1, abs(r2-r) + abs(a[r]-c2)});
if (a[r] < c2) break;
}
reprl(r, r2, 0) {
gp[r].pb({2*n+1, abs(r2-r) + (c2-0)});
gp[r+n].pb({2*n+1, abs(r2-r) + abs(a[r]-c2)});
if (a[r] < c2) break;
}
/*rep(u, n) {*/
/* cout << "FROM " << u << ": " << endl;*/
/* for (auto[v, w] : gp[u]) cout << " TO " << v << " WITH WEIGHT " << w << endl;*/
/*}*/
/*rep(u, n) {*/
/* cout << "FROM " << u+n << ": " << endl;*/
/* for (auto[v, w] : gp[u+n]) cout << " TO " << v << " WITH WEIGHT " << w << endl;*/
/*}*/
int N = 2*n+2;
priority_queue<PLL> pq;
VI vis(N);
VL dist(N, 1e18);
dist[2*n] = 0;
pq.push({-dist[2*n], 2*n});
while (pq.size()) {
int u = pq.top().ss;
pq.pop();
if (vis[u]) continue;
vis[u] = true;
/*cout << "PROCESSING " << u << endl;*/
for (auto[v, w] : gp[u]) {
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.push({-dist[v], v});
}
}
}
/*rep(u, N) {*/
/* for (auto[v, w] : gp[u]) {*/
/* if (v == 2*n+1) {*/
/* cout << u << " " << v << " " << w << endl;*/
/* }*/
/* }*/
/*}*/
ll straight = abs(r1-r2) + abs(c1-c2);
replr(i, min(r1, r2), max(r1, r2)) {
if (a[i] < min(c1, c2)) {
straight = 1e18;
}
}
cout << min(straight, dist[2*n+1]) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
/*freopen("mincross.in", "r", stdin); */
/*freopen("test.out", "w", stdout); */
int t = 1;
/*cin >> t; */
while (t--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void solve()':
Main.cpp:131:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
131 | for (auto[v, w] : gp[u]) {
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |