이 제출은 이전 버전의 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;
ll solve2(int n, int r1, int c1, int r2, int c2, VI a) {
r1--, c1--, r2--, c2--;
priority_queue<pair<ll, PII>> pq;
map<PII, int> vis;
map<PII, ll> dist;
pq.push({0, {r1, c1}});
while (pq.size()) {
PII u = pq.top().ss;
pq.pop();
if (vis[u]) continue;
vis[u] = true;
vector<pair<PII, ll>> harevan;
if (true) {
auto[r, c] = u;
if (r) {
harevan.pb({{r-1, min(c, a[r-1])}, 1});
if (c == 0) harevan.pb({{r-1, a[r-1]}, 1});
}
if (r != n-1) {
harevan.pb({{r+1, min(c, a[r+1])}, 1});
if (c == a[r]) harevan.pb({{r+1, 0}, 1});
}
harevan.pb({{r, a[r]}, a[r]-c});
harevan.pb({{r, 0}, c});
}
for (auto[v, w] : harevan) {
if (!dist.count(v) || dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.push({-dist[v], v});
}
}
}
ll ans = 1e18;
for (auto[u, d] : dist) {
if (u.ff == r2) {
/*cout << "Distance to (" << u.ff << ", " << u.ss << ") = " << d << endl;*/
setmin(ans, 1ll * abs(u.ss - c2) + d);
}
}
return ans;
}
ll solve1(int n, int r1, int c1, int r2, int c2, VI a) {
r1--, c1--, r2--, c2--;
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});
}
rep(u, n) {
gp[u].pb({u+n, a[u]});
gp[u+n].pb({u, a[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 << ", DISTANCE " << dist[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;
}
}
return min(straight, dist[2*n+1]);
}
void grader() {
int n;
cin >> n;
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
VI a(n);
for (int& x : a) cin >> x;
cout << solve1(n, r1, c1, r2, c2, a) << endl;
/*ll ans1 = solve1(n, r1, c1, r2, c2, a);*/
/*ll ans2 = solve2(n, r1, c1, r2, c2, a);*/
/*cout << "ANSWERS: " << ans1 << " " << ans2 << endl;*/
return;
int t;
cin >> t;
while (t--) {
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
int n = 2;
VI a(n);
for (int& x : a) x = rng()%10;
int r1 = rng()%n+1;
int c1 = rng()%(a[r1-1]+1)+1;
int r2 = rng()%n+1;
int c2 = rng()%(a[r2-1]+1)+1;
cout << endl;
cout << n << endl;
cout << r1 << " " << c1 << endl;
cout << r2 << " " << c2 << endl;
for (int x : a) cout << x << " "; cout << endl;
ll ans1 = solve1(n, r1, c1, r2, c2, a);
ll ans2 = solve2(n, r1, c1, r2, c2, a);
cout << "ANSWERS: " << ans1 << " " << ans2 << endl;
if (ans1 != ans2) {
rep(i, 100) cout << "VAX QU ";
return;
}
}
}
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--) grader();
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'll solve2(int, int, int, int, int, VI)':
Main.cpp:50:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | auto[r, c] = u;
| ^
Main.cpp:62:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for (auto[v, w] : harevan) {
| ^
Main.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
70 | for (auto[u, d] : dist) {
| ^
Main.cpp: In function 'll solve1(int, int, int, int, int, VI)':
Main.cpp:171:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
171 | for (auto[v, w] : gp[u]) {
| ^
Main.cpp: In function 'void grader()':
Main.cpp:222:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
222 | for (int x : a) cout << x << " "; cout << endl;
| ^~~
Main.cpp:222:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
222 | for (int x : a) cout << x << " "; cout << endl;
| ^~~~
# | 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... |