Submission #1047206

#TimeUsernameProblemLanguageResultExecution timeMemory
1047206VahanAbrahamText editor (CEOI24_editor)C++17
0 / 100
4 ms33116 KiB
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <algorithm> #include <cstring> #include <cstdio> #include <sstream> #include <map> #include <stack> #include <set> #include <queue> #include <list> #include <unordered_set> #include <unordered_map> #include <math.h> #include <bitset> #include <cmath> #include <vector> #include <iomanip> #include <random> #include <chrono> #include <cassert> using namespace std; #define ll long long #define fr first #define sc second #define pb push_back #define US freopen(".in", "r", stdin); freopen("j.out", "w", stdout); ll gcd(ll a, ll b) { if (a == 0 || b == 0) { return max(a, b); } if (a <= b) { return gcd(a, b % a); } else { return gcd(a % b, b); } } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } const int N = 1000005; const ll oo = 100000000000000000, MOD = 1000000007; int l[N]; stack<ll> st; vector<pair<int, ll>> g[N]; int usstack(ll x) { while (st.empty() != 1) { int ind = st.top(); if (l[ind] < x) { return ind; } st.pop(); } return -1; } void maqstack() { while (st.empty() != 1) { st.pop(); } } ll dist[2 * N], her[2 * N]; int sindnerq[N], sindver[N]; void solve() { int n; ll xs, ys, xe, ye; cin >> n >> xs >> ys >> xe >> ye; for (int i = 1; i <= n; ++i) { cin >> l[i]; } st.push(1); for (int i = 2; i <= n; ++i) { int ham = (i - 1) * 2 + 1; g[ham].pb({ ham - 1,1 }); g[ham - 1].pb({ ham,1 }); g[ham].pb({ ham - 2,1 }); g[ham - 2].pb({ ham,1 }); ++ham; int ind = usstack(l[i]); if (ind != -1) { g[ham].pb({ ind * 2,i - ind }); } st.push(i); } maqstack(); for (int i = 1; i <= xs; ++i) { if (i == xs) { sindnerq[i] = usstack(ys) + 1; if (sindnerq[i] == 0) { sindnerq[i] = 1; } } else { int ind = usstack(l[i]); st.push(i); } } maqstack(); for (int i = n; i >= xs; --i) { if (i == xs) { sindver[i] = usstack(ys) - 1; if (sindver[i] == -2) { sindver[i] = n; } } else { int ind = usstack(l[i]); st.push(i); } } priority_queue<pair<ll, int>> pq; for (int i = 1; i <= 2 * n; ++i) { dist[i] = oo; } for (int i = sindnerq[xs]; i <= sindver[xs]; ++i) { dist[i * 2 - 1] = abs(xs - i) + ys - 1; pq.push({ -dist[i * 2 - 1],i * 2 - 1 }); dist[i * 2] = abs(xs - i) + l[i] - ys; pq.push({ -dist[i * 2],i * 2 }); } int ind = sindver[xs] + 1; if (ind <= n) { dist[ind * 2] = abs(xs - ind); pq.push({ -dist[ind * 2],ind * 2 }); } ind = sindnerq[xs] - 1; if (ind >= 1) { dist[ind * 2] = abs(xs - ind); pq.push({ -dist[ind * 2],ind * 2 }); } while (pq.empty() != 1) { int x = (pq.top()).sc; pq.pop(); for (int i = 0; i < g[x].size(); ++i) { int h = g[x][i].fr; ll w = g[x][i].sc; if (dist[h] > dist[x] + w) { dist[h] = dist[x] + w; pq.push({ -dist[h],h }); } } } maqstack(); for (int i = 1; i <= 2 * n; ++i) { her[i] = oo; } for (int i = 1; i <= n; ++i) { int ind = usstack(l[i]) + 1; if (ind == 0) { ind = 1; } if (ind >= xe && ind <= i) { her[i * 2] = i - xe + abs(ye - (l[i] + 1)); } her[i * 2 - 1] = abs(xe - i) + abs(ye - 1); st.push(i); } maqstack(); for (int i = n; i >= 1; --i) { int ind = usstack(l[i]) - 1; if (ind == -2) { ind = n; } if (xe >= i && xe <= ind) { her[i * 2] = xe - i + abs(ye - (l[i] + 1)); } st.push(i); } ll pat = oo; if (sindnerq[xs] <= xe && xe <= sindver[xs]) { pat = abs(xe - xs) + abs(ye - ys); } for (int i = 1; i <= 2 * n; ++i) { //cout << i << " " << dist[i] << " " << her[i] << " " << dist[i] + her[i] << endl; pat = min(pat, dist[i] + her[i]); } cout << pat << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //US int tt = 1; //cin >> tt; while (tt--) { solve(); } }

Compilation message (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:105:17: warning: unused variable 'ind' [-Wunused-variable]
  105 |             int ind = usstack(l[i]);
      |                 ^~~
Main.cpp:118:17: warning: unused variable 'ind' [-Wunused-variable]
  118 |             int ind = usstack(l[i]);
      |                 ^~~
Main.cpp:145:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |         for (int i = 0; i < g[x].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~
#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...