Submission #413027

# Submission time Handle Problem Language Result Execution time Memory
413027 2021-05-28T05:17:47 Z Berted Climbers (RMI18_climbers) C++14
100 / 100
346 ms 196164 KB
#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

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 time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 8 ms 8052 KB Output is correct
4 Correct 50 ms 70708 KB Output is correct
5 Correct 165 ms 195956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 948 KB Output is correct
3 Correct 99 ms 8164 KB Output is correct
4 Correct 210 ms 70824 KB Output is correct
5 Correct 260 ms 70808 KB Output is correct
6 Correct 265 ms 125608 KB Output is correct
7 Correct 293 ms 125508 KB Output is correct
8 Correct 292 ms 196164 KB Output is correct
9 Correct 340 ms 196064 KB Output is correct
10 Correct 346 ms 196076 KB Output is correct