Submission #227064

#TimeUsernameProblemLanguageResultExecution timeMemory
227064qkxwsmClimbers (RMI18_climbers)C++14
0 / 100
190 ms196600 KiB
#include <bits/stdc++.h> using namespace std; template<class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template<class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define SZ(x) ((int) (x).size()) #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; typedef pair<ll, pii> plp; const int MAXN = 5013; const ll LLINF = 2e18; int N; vl arr; ll dist[MAXN][MAXN]; priority_queue<plp, vector<plp>, greater<plp> > pq; ll ans = LLINF; bool between(int a, int b, int c) { return ((b <= a && a <= c) || (c <= a && a <= b)); } int32_t main() { cout << fixed << setprecision(12); cerr << fixed << setprecision(4); ios_base::sync_with_stdio(false); cin.tie(0); cin >> N; FOR(i, 0, N) { ll x; cin >> x; if (SZ(arr) >= 2) { if (arr[SZ(arr) - 2] <= arr.back() && arr.back() <= x) { arr.pop_back(); } else if (arr[SZ(arr) - 2] >= arr.back() && arr.back() >= x) { arr.pop_back(); } } arr.PB(x); } N = SZ(arr); FOR(i, 0, N) { fill(dist[i], dist[i] + N, LLINF); } dist[0][N - 2] = 0; pq.push({0, {0, N - 2}}); while(!pq.empty()) { ll d = pq.top().fi; pii u = pq.top().se; pq.pop(); //one of you is at u.fi, the other is at arr[u.fi] between u.se and u.se + 1 ll nd; pii v; if (u.se + 1 != N - 1 && arr[u.se + 1] == arr[u.fi]) { nd = d; v = {u.fi, u.se + 1}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } if (u.se != 0 && arr[u.se] == arr[u.fi]) { nd = d; v = {u.fi, u.se - 1}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } //right guy moves to .se + 1 if (u.fi != N - 1 && between(arr[u.se + 1], arr[u.fi], arr[u.fi + 1])) { nd = d + abs(arr[u.se + 1] - arr[u.fi]); v = {u.se + 1, u.fi}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } if (u.fi != 0 && between(arr[u.se + 1], arr[u.fi], arr[u.fi - 1])) { nd = d + abs(arr[u.se + 1] - arr[u.fi]); v = {u.se + 1, u.fi - 1}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } //right guy moves to .se if (u.fi != N - 1 && between(arr[u.se], arr[u.fi], arr[u.fi + 1])) { nd = d + abs(arr[u.se] - arr[u.fi]); v = {u.se, u.fi}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } if (u.fi != 0 && between(arr[u.se + 1], arr[u.fi], arr[u.fi - 1])) { nd = d + abs(arr[u.se] - arr[u.fi]); v = {u.se, u.fi - 1}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } //left guy moves right. if (u.fi != N - 1 && between(arr[u.fi + 1], arr[u.se], arr[u.se + 1])) { nd = d + abs(arr[u.fi + 1] - arr[u.fi]); v = {u.fi + 1, u.se}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } if (u.fi != 0 && between(arr[u.fi - 1], arr[u.se], arr[u.se + 1])) { nd = d + abs(arr[u.fi - 1] - arr[u.fi]); v = {u.fi - 1, u.se}; if (dist[v.fi][v.se] > nd) { dist[v.fi][v.se] = nd; pq.push({nd, v}); } } } FOR(i, 0, N) { ckmin(ans, dist[i][i]); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...