제출 #1237426

#제출 시각아이디문제언어결과실행 시간메모리
1237426GrayText editor (CEOI24_editor)C++20
56 / 100
4110 ms549972 KiB
#pragma GCC optimize("Ofast,unroll-loops") #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){ ll res=lower_bound(pts.begin(), pts.end(), make_pair(r, c))-pts.begin(); if(res>=(ll)pts.size() or pts[res]!=make_pair(r, c)){ return -1; } return res; }; vector<vector<pair<ll, ll>>> A(pts.size()); vector<ll> st, 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){ for (ll j=0; j<(ll)con.size(); j++){ if (con[j]<=a[i-1]) { ll from = getid(i, con[j]), to = getid(i+1, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } } ll 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){ for (ll j=0; j<(ll)con.size(); j++){ if (con[j]<=a[i+1]) { ll from = getid(i+1, con[j]), to = getid(i+2, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } } } 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++){ 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()); 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; while (!que.empty()){ auto cur = que.top(); que.pop(); ll u = cur.ss, sw = cur.ff; if (vis[u]) continue; 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[getid(er, ec)] << 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...