Submission #1047294

#TimeUsernameProblemLanguageResultExecution timeMemory
1047294VahanAbrahamText editor (CEOI24_editor)C++14
100 / 100
840 ms331448 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<int> st;
vector<pair<int, ll>> g[2*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];
    }
    for (int i = 1; i <= 2 * n; ++i) {
        dist[i] = oo, her[i] = oo;
    }
    priority_queue<pair<ll, int>> pq;
    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) {
            if (ind == n) {
                g[ham].pb({ ind * 2-1,i - ind });
            }
            if (i == n) {
                g[ham-1].pb({ ind * 2,i - ind });
            }
            g[ham].pb({ ind * 2,i - ind });
        }
        st.push(i);
    }
    maqstack();
    for (int i = n; i >= 1; --i) {
        int ind = usstack(l[i]);
        if (ind != -1) {
            if (i == n) {
                g[i * 2-1].pb({ ind * 2,ind - i });
            }
            g[i*2].pb({ ind * 2,ind - i });
            if (ind == n) {
                g[i * 2].pb({ ind * 2-1,ind - i });
            }
        }
        st.push(i);
    }
    maqstack();
    for (int i = 1; i <= xs; ++i) {
        if (i == xs) {
            sindnerq[i] = usstack(ys-1) + 1;
            if (sindnerq[i] == 0) {
                sindnerq[i] = 1;
            }
            else {
                int index = sindnerq[i] - 1;
                dist[2*index] = abs(xs - index);
                pq.push({ -dist[2 * index],2*index });
                if (index == n) {
                    dist[2 * index - 1] = abs(xs - index);
                    pq.push({ -dist[2 * index - 1],2 * index - 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) - 1;
            if (sindver[i] == -2) {
                sindver[i] = n;
            }
            else {
                int index = sindver[i] + 1;
                dist[2 * index] = abs(xs - index);
                pq.push({ -dist[2 * index],2*index });
                if (index == n) {
                    dist[2 * index-1] = abs(xs - index);
                    pq.push({ -dist[2 * index-1],2*index-1 });
                }
            }
        }
        else {
            int ind = usstack(l[i]);
            st.push(i);
        }
    }
    for (int i = sindnerq[xs]; i <= sindver[xs]; ++i) {
        if (dist[i * 2 - 1] > abs(xs - i) + ys - 1) {
            dist[i * 2 - 1] = abs(xs - i) + ys - 1;
            pq.push({ -dist[i * 2 - 1],i * 2 - 1 });
        }
        if (dist[i * 2] > abs(xs - i) + l[i]+1 - ys) {
            dist[i * 2] = abs(xs - i) + l[i]+1 - ys;
            pq.push({ -dist[i * 2],i * 2 });
        }
    }
    //cout << "BRRR" << " " << dist[1] << " " << dist[4] << endl;
    /*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 });
            }
        }
    }
    //cout << "CSS" << " " << dist[1] << endl;
    maqstack();
    for (int i = 1; i <= n; ++i) {
        int ind = usstack(l[i]) + 1;
        if (ind == 0) {
            ind = 1;
        }
        if (ind <= xe && xe <= 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] = min(xe - i + abs(ye - (l[i] + 1)), her[i * 2]);
        }
        st.push(i);
    }
    ll pat = oo;
    if (sindnerq[xs] <= xe && xe <= sindver[xs]) {
        pat = abs(xe - xs) + abs(ye - ys);
    }
   /* cout << her[4] << " " << her[3] << " " << her[1] << " " << her[2] << endl;
    cout << "GOO " << " " << dist[4] << endl;*/
    for (int i = 1; i <= 2 * n; ++i) {
        //cout << i << " " << dist[i] << " " << her[i] << " " << dist[i] + her[i] << endl;
        /*if (i >= 2 * n - 1) {
            pat = min(pat, min(dist[i], dist[i + 1]) + min(her[i], her[i + 1]));
        }
        else {
            pat = min(pat, dist[i] + her[i]);
        }*/
        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:138:17: warning: unused variable 'ind' [-Wunused-variable]
  138 |             int ind = usstack(l[i]);
      |                 ^~~
Main.cpp:160:17: warning: unused variable 'ind' [-Wunused-variable]
  160 |             int ind = usstack(l[i]);
      |                 ^~~
Main.cpp:188: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]
  188 |         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...