Submission #856767

#TimeUsernameProblemLanguageResultExecution timeMemory
856767Tenis0206Climbers (RMI18_climbers)C++11
100 / 100
236 ms413008 KiB
#include <bits/stdc++.h> using namespace std; const long long oo = LLONG_MAX; const int nmax = 5e3; int n; int v[nmax + 5]; long long d[nmax + 5][nmax + 5][2]; bool sel[nmax + 5][nmax + 5][2]; typedef pair<pair<long long,int>,pair<int,int>> pii; priority_queue<pii,vector<pii>,greater<pii>> h; bool inclus(int a, int x, int y) { if(x > y) { swap(x, y); } return (x <= a && a <= y); } int main() { ios::sync_with_stdio(false); cin.tie(0); #ifdef home freopen("nr.in","r",stdin); freopen("nr.out","w",stdout); #endif // home cin>>n; for(int i=1;i<=n;i++) { cin>>v[i]; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { d[i][j][0] = d[i][j][1] = oo; } } d[1][n][0] = d[1][n - 1][1] = 0; h.push({{0,0},{1,n}}); h.push({{0,1},{1,n-1}}); while(!h.empty()) { while(!h.empty() && sel[h.top().second.first][h.top().second.second][h.top().first.second]) { h.pop(); } if(h.empty()) { break; } int tip = h.top().first.second; int st = h.top().second.first; int dr = h.top().second.second; if(tip==0 && st==dr-1) { cout<<h.top().first.first<<'\n'; return 0; } if(tip==1 && st==dr) { cout<<h.top().first.first<<'\n'; return 0; } h.pop(); sel[st][dr][tip] = true; if(tip==0) { /// st e intre pozitii /// st se duce la stanga, dr se duce la stanga if(dr!=1 && inclus(v[st], v[dr - 1], v[dr])) { if(d[st][dr][0] + abs(v[st] - v[dr]) < d[st][dr - 1][1]) { d[st][dr - 1][1] = d[st][dr][0] + abs(v[st] - v[dr]); h.push({{d[st][dr - 1][1],1},{st, dr - 1}}); } } /// st se duce la stanga, dr se duce la dreapta if(dr!=n && inclus(v[st], v[dr], v[dr + 1])) { if(d[st][dr][0] + abs(v[st] - v[dr]) < d[st][dr][1]) { d[st][dr][1] = d[st][dr][0] + abs(v[st] - v[dr]); h.push({{d[st][dr][1],1},{st,dr}}); } } /// st se duce la dreapta, dr se duce la stanga if(dr!=1 && inclus(v[st + 1], v[dr - 1], v[dr])) { if(d[st][dr][0] + abs(v[st + 1] - v[dr]) < d[st + 1][dr - 1][1]) { d[st + 1][dr - 1][1] = d[st][dr][0] + abs(v[st + 1] - v[dr]); h.push({{d[st + 1][dr - 1][1], 1},{st + 1, dr - 1}}); } } /// st se duce la dreapta, dr se duce la dreapta if(dr!=n && inclus(v[st + 1], v[dr], v[dr + 1])) { if(d[st][dr][0] + abs(v[st + 1] - v[dr]) < d[st + 1][dr][1]) { d[st + 1][dr][1] = d[st][dr][0] + abs(v[st + 1] - v[dr]); h.push({{d[st + 1][dr][1], 1},{st + 1, dr}}); } } /// dr se duce la stanga if(dr!=1 && inclus(v[dr - 1], v[st], v[st + 1])) { if(d[st][dr][0] + abs(v[dr - 1] - v[dr]) < d[st][dr - 1][0]) { d[st][dr - 1][0] = d[st][dr][0] + abs(v[dr - 1] - v[dr]); h.push({{d[st][dr - 1][0], 0},{st,dr-1}}); } } /// dr se duce la dreapta if(dr!=n && inclus(v[dr + 1], v[st], v[st + 1])) { if(d[st][dr][0] + abs(v[dr + 1] - v[dr]) < d[st][dr + 1][0]) { d[st][dr + 1][0] = d[st][dr][0] + abs(v[dr + 1] - v[dr]); h.push({{d[st][dr + 1][0], 0},{st, dr + 1}}); } } } else { /// dr e intre pozitii /// dr se duce la stanga, st se duce la stanga if(st!=1 && inclus(v[dr], v[st], v[st - 1])) { if(d[st][dr][1] + abs(v[dr] - v[st]) < d[st - 1][dr][0]) { d[st - 1][dr][0] = d[st][dr][1] + abs(v[dr] - v[st]); h.push({{d[st - 1][dr][0], 0},{st - 1, dr}}); } } /// dr se duce la stanga, st se duce la dreapta if(st!=n && inclus(v[dr], v[st], v[st + 1])) { if(d[st][dr][1] + abs(v[dr] - v[st]) < d[st][dr][0]) { d[st][dr][0] = d[st][dr][1] + abs(v[dr] - v[st]); h.push({{d[st][dr][0],0},{st,dr}}); } } /// dr se duce la dreapta, st se duce la stanga if(st!=1 && inclus(v[dr + 1], v[st],v[st - 1])) { if(d[st][dr][1] + abs(v[dr + 1] - v[st]) < d[st - 1][dr + 1][0]) { d[st - 1][dr + 1][0] = d[st][dr][1] + abs(v[dr + 1] - v[st]); h.push({{d[st - 1][dr + 1][0], 0},{st - 1, dr + 1}}); } } /// dr se duce la dreapta, st se duce la dreapta if(st!=n && inclus(v[dr + 1], v[st], v[st + 1])) { if(d[st][dr][1] + abs(v[dr + 1] - v[st]) < d[st][dr + 1][0]) { d[st][dr + 1][0] = d[st][dr][1] + abs(v[dr + 1] - v[st]); h.push({{d[st][dr + 1][0], 0},{st, dr + 1}}); } } /// st se duce la stanga if(st!=1 && inclus(v[st - 1], v[dr], v[dr + 1])) { if(d[st][dr][1] + abs(v[st - 1] - v[st]) < d[st - 1][dr][1]) { d[st - 1][dr][1] = d[st][dr][1] + abs(v[st - 1] - v[st]); h.push({{d[st - 1][dr][1], 1},{st - 1, dr}}); } } /// st se duce la dreapta if(st!=n && inclus(v[st + 1], v[dr], v[dr + 1])) { if(d[st][dr][1] + abs(v[st + 1] - v[st]) < d[st + 1][dr][1]) { d[st + 1][dr][1] = d[st][dr][1] + abs(v[st + 1] - v[st]); h.push({{d[st + 1][dr][1], 1},{st + 1, dr}}); } } } } cout<<"NO"<<'\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...