Submission #413027

#TimeUsernameProblemLanguageResultExecution timeMemory
413027BertedClimbers (RMI18_climbers)C++14
100 / 100
346 ms196164 KiB
#include <iostream> #include <queue> #define ll long long #define pii pair<ll, int> #define fst first #define snd second using namespace std; const ll INF = 1e15; int N, A[5001]; ll dist[25500001]; priority_queue<pii, vector<pii>, greater<pii>> pq; inline pair<int, int> getID(const int &x) { int L = 1, R = N; while (L < R) { int M = L + R >> 1; if (x < N * (N + 1) - M * (M + 1)) {L = M + 1;} else {R = M;} } return {N - L, (x - N * (N + 1) + L * (L + 1)) / 2 + N - L}; } inline int revID(const int &l, const int &r, const int &s) { return N * (N + 1) - (N - l) * (N - l + 1) + 2 * (r - l) + s; } inline bool check(int l, int m, int r) {return min(A[l], A[r]) <= A[m] && A[m] <= max(A[l], A[r]);} inline void insert(int l, int gt, int r, int p, bool b, ll d) { if (gt < N && gt >= 0 && l >= 0 && r < N && check(l, gt, r)) { int nex = (b) ? revID(l, gt, 1) : revID(gt, r, 0); if (d + abs(A[gt] - A[p]) < dist[nex]) { dist[nex] = d + abs(A[gt] - A[p]); pq.push({dist[nex], nex}); } } } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; for (int i = 0; i < N * (N + 1); i++) dist[i] = INF; dist[revID(0, N - 1, 0)] = 0; pq.push({0, revID(0, N - 1, 0)}); while (pq.size()) { if (dist[pq.top().snd] == pq.top().fst) { pair<int, int> u = getID(pq.top().snd); int s = pq.top().snd % 2; ll d = pq.top().fst; pq.pop(); if (u.fst == u.snd) {cout << d << "\n"; return 0;} if (s) { insert(u.fst, u.snd - 1, u.fst + 1, u.snd, 1, d); insert(u.fst, u.snd + 1, u.fst + 1, u.snd, 1, d); insert(u.snd - 1, u.fst + 1, u.snd, u.snd, 0, d); insert(u.snd - 1, u.fst , u.snd, u.snd, 0, d); insert(u.snd, u.fst + 1, u.snd + 1, u.snd, 0, d); insert(u.snd, u.fst , u.snd + 1, u.snd, 0, d); } else { insert(u.snd - 1, u.fst + 1, u.snd, u.fst, 0, d); insert(u.snd - 1, u.fst - 1, u.snd, u.fst, 0, d); insert(u.fst, u.snd - 1, u.fst + 1, u.fst, 1, d); insert(u.fst, u.snd , u.fst + 1, u.fst, 1, d); insert(u.fst - 1, u.snd - 1, u.fst, u.fst, 1, d); insert(u.fst - 1, u.snd , u.fst, u.fst, 1, d); } } else {pq.pop();} } cout << "-1\n"; return 0; }

Compilation message (stderr)

climbers.cpp: In function 'std::pair<int, int> getID(const int&)':
climbers.cpp:21:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |   int M = L + R >> 1;
      |           ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...