제출 #1237443

#제출 시각아이디문제언어결과실행 시간메모리
1237443GrayText editor (CEOI24_editor)C++20
5 / 100
2 ms840 KiB
#pragma GCC optimize("unroll-loops,Ofast") #pragma GCC target("avx,avx2") #include<bits/stdc++.h> #define ll int #define ff first #define ss second #define ln "\n" using namespace std; const ll INF = 2e9; void solve(){ ll n; cin >> n; ll sr, sc; cin >> sr >> sc; ll er, ec; cin >> er >> ec; vector<ll> a(n); for (ll i=0; i<n; i++){ cin >> a[i]; a[i]++; } vector<pair<ll, ll>> pts; pts.push_back({er, ec}); pts.push_back({sr, sc}); for (ll i=0; i<n; i++){ pts.push_back({i+1, 1}); pts.push_back({i+1, a[i]}); if (a[i]>sc and sc>1) pts.push_back({i+1, sc}); if (a[i]>ec and ec>1) pts.push_back({i+1, ec}); if (i+1<n and a[i+1]<=a[i]) pts.push_back({i+1, a[i+1]}); if (i and a[i-1]<=a[i]) pts.push_back({i+1, a[i-1]}); } sort(pts.begin(), pts.end()); pts.erase(unique(pts.begin(), pts.end()), pts.end()); auto getid = [&](ll r, ll c){ return lower_bound(pts.begin(), pts.end(), make_pair(r, c))-pts.begin(); }; vector<vector<pair<ll, ll>>> A(pts.size()); vector<ll> con; vector<vector<pair<ll, ll>>> sedge1(n); for (ll i=n-1; i>=0; i--){ con.clear(); con.push_back(1); con.push_back(a[i]); if (a[i]>sc and sc>1) con.push_back(sc); if (a[i]>ec and ec>1) con.push_back(ec); if (i+1<n and a[i+1]<=a[i]) con.push_back(a[i+1]); if (i and a[i-1]<=a[i]) con.push_back(a[i-1]); sort(con.begin(), con.end()); con.erase(unique(con.begin(), con.end()), con.end()); for (ll j=1; j<(ll)con.size(); j++){ ll from = getid(i+1, con[j-1]), to = getid(i+1, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, con[j]-con[j-1]}); A[to].push_back({from, con[j]-con[j-1]}); } } if (i){ ll from = getid(i, 1), to = getid(i+1, 1); A[from].push_back({to, 1}); A[to].push_back({from, 1}); from = getid(i, min(a[i], a[i-1])), to = getid(i+1, min(a[i], a[i-1])); A[from].push_back({to, 1}); A[to].push_back({from, 1}); from = getid(i, a[i-1]), to = getid(i+1, 1); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } if (i+1<n){ ll from = getid(i+2, 1), to = getid(i+1, 1); A[from].push_back({to, 1}); A[to].push_back({from, 1}); from = getid(i+2, min(a[i], a[i+1])), to = getid(i+1, min(a[i], a[i+1])); A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } vector<ll> st; for (ll i=n-1; i>=0; i--){ while(!st.empty() and a[st.back()]>=a[i]) st.pop_back(); if (!st.empty()){ ll lim=a[st.back()]; sedge1[i].push_back({lim, st.back()}); } st.push_back(i); } st.clear(); for (ll i=0; i<n; i++){ while(!st.empty() and a[st.back()]>=a[i]) st.pop_back(); if (!st.empty()){ ll lim=a[st.back()]; sedge1[i].push_back({lim, st.back()}); } st.push_back(i); } priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que; vector<ll> dist(pts.size(), INF), vis(pts.size()); ll root=getid(sr, sc); que.push({0, root}); dist[root]=0; ll dest=getid(er, ec); while (!que.empty()){ auto cur = que.top(); que.pop(); ll u = cur.ss, sw = cur.ff; if (vis[u]) continue; if (u==dest) break; vis[u]=1; pair<ll, ll> cor = pts[u]; ll i = cor.ff-1, j=cor.ss; for (auto [lim, ind]:sedge1[i]){ if (lim<j){ ll lup = getid(ind+1, a[ind]); ll w = abs(i-ind); if (dist[lup]>sw+w){ dist[lup]=sw+w; que.push({sw+w, lup}); } } } for (auto [v, w]:A[u]){ if (dist[v]>w+sw){ que.push({w+sw, v}); dist[v]=w+sw; } } } cout << dist[dest] << ln; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); ll t=1; // cin >> t; while (t--){ solve(); } }
#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...