Submission #1047589

#TimeUsernameProblemLanguageResultExecution timeMemory
1047589c2zi6Text editor (CEOI24_editor)C++14
100 / 100
667 ms292784 KiB
#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 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)}); } int mn; mn = a[r2]; replr(r, r2, n-1) { setmin(mn, a[r]); if (mn < min(c2, a[r])) continue; 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)}); } mn = a[r2]; reprl(r, r2, 0) { setmin(mn, a[r]); if (mn < min(c2, a[r])) continue; 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)}); } 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; for (auto[v, w] : gp[u]) { if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.push({-dist[v], v}); } } } 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; } 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(); }

Compilation message (stderr)

Main.cpp: In function 'll solve1(int, int, int, int, int, VI)':
Main.cpp:122:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  122 |         for (auto[v, w] : gp[u]) {
      |                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...