Submission #48167

#TimeUsernameProblemLanguageResultExecution timeMemory
48167E869120Jakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1090 ms92412 KiB
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;

int N, M, A[30009], P[30009], dist[7400009], sz; pair<int, int>vec[2400009];
vector<int> G[30009], X[2400009];
map<pair<int, int>, int>M1; queue<int>Q;

void connect(int px, int py, int qx, int qy, int cost) {
	int pos1 = lower_bound(vec, vec + sz, make_pair(px, py)) - vec;
	int pos2 = lower_bound(vec, vec + sz, make_pair(qx, qy)) - vec;
	X[pos1].push_back(pos2);
}

int main() {
	cin >> N >> M;
	for (int i = 1; i <= M; i++) {
		cin >> A[i] >> P[i]; G[A[i]].push_back(P[i]);
		if (M1[make_pair(A[i] % P[i], P[i])] == 0) {
			M1[make_pair(A[i] % P[i], P[i])] = 1;
			for (int j = A[i] % P[i]; j < N; j += P[i]) { vec[sz] = make_pair(j, P[i]); sz++; }
		}
	}
	for (int i = 0; i < N; i++) { vec[sz] = make_pair(i, 0); sz++; }
	sort(vec, vec + sz);

	for (int i = 0; i < sz; i++) {
		int pos = vec[i].first, state = vec[i].second;

		if (state == 0) {
			for (int j = 0; j < G[pos].size(); j++) {
				if (pos - G[pos][j] >= 0) {
					connect(pos, 0, pos - G[pos][j], G[pos][j], 1);
					connect(pos, 0, pos - G[pos][j], 0, 1);
				}
				if (pos + G[pos][j] < N) {
					connect(pos, 0, pos + G[pos][j], G[pos][j], 1);
					connect(pos, 0, pos + G[pos][j], 0, 1);
				}
			}
		}
		else {
			if (pos - state >= 0) {
				connect(pos, state, pos - state, state, 1);
				connect(pos, state, pos - state, 0, 1);
			}
			if (pos + state < N) {
				connect(pos, state, pos + state, state, 1);
				connect(pos, state, pos + state, 0, 1);
			}
		}
	}
	for (int i = 0; i < sz; i++) dist[i] = (1 << 30);
	int sx = lower_bound(vec, vec + sz, make_pair(A[1], 0)) - vec;
	int gx = lower_bound(vec, vec + sz, make_pair(A[2], 0)) - vec;
	dist[sx] = 0; Q.push(sx);

	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();

		for (int i = 0; i < X[pos].size(); i++) {
			int to = X[pos][i];
			if (dist[to] > dist[pos] + 1) {
				dist[to] = dist[pos] + 1;
				Q.push(to);
			}
		}
	}
	int ans = dist[gx]; if (ans == (1 << 30)) ans = -1;
	cout << ans << endl;
	return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:36:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j = 0; j < G[pos].size(); j++) {
                    ~~^~~~~~~~~~~~~~~
skyscraper.cpp:66:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < X[pos].size(); i++) {
                   ~~^~~~~~~~~~~~~~~
#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...