제출 #435303

#제출 시각아이디문제언어결과실행 시간메모리
435303sinamhdvJakarta Skyscrapers (APIO15_skyscraper)C++11
57 / 100
421 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

#define MAXN 30100
#define SQRT 180
#define BLK (MAXN / SQRT) + 10

int n, m;
int B[MAXN], p[MAXN];
vector<int> hab[MAXN];
bool mark[MAXN][SQRT];
int dist[MAXN][SQRT + 1];
vector<pair<pii, int>> adj[MAXN][SQRT + 1];

inline void addedge(int x1, int y1, int x2, int y2, int w)
{
	adj[x1][y1].pb({{x2, y2}, w});
}

void dij(void)
{
	priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> s;
	FOR(i, 0, MAXN) fill(dist[i], dist[i] + SQRT + 1, INF);
	dist[B[1]][0] = 0;
	s.push({0, {B[1], 0}});
	while (!s.empty())
	{
		auto pr = s.top();
		s.pop();
		int v = pr.sc.fr;
		int p = pr.sc.sc;
		if (dist[v][p] != pr.fr) continue;
		for (auto pu : adj[v][p])
		{
			int u = pu.fr.fr;
			int up = pu.fr.sc;
			int w = pu.sc;
			if (dist[v][p] + w >= dist[u][up]) continue;
			dist[u][up] = dist[v][p] + w;
			s.push({dist[u][up], {u, up}});
		}
	}
}

int32_t main(void)
{
	fast_io;
	cin >> n >> m;
	FOR(i, 1, m + 1)
	{
		cin >> B[i] >> p[i];
		B[i]++;
		hab[B[i]].pb(i);
		if (p[i] < SQRT) mark[B[i]][p[i]] = true;
	}

	FOR(i, 1, n + 1)
	{
		FOR(j, 1, SQRT)
		{
			addedge(i, j, i, 0, 0);
			if (mark[i][j]) addedge(i, 0, i, j, 0);

			if (i + j <= n) addedge(i, j, i + j, j, 1);
			if (i - j >= 1) addedge(i, j, i - j, j, 1);
		}
		for (int u : hab[i]) if (p[u] >= SQRT)
		{
			addedge(i, 0, u, SQRT, 0);
		}
	}

	FOR(i, 1, m + 1) if (p[i] >= SQRT)
	{
		for (int j = B[i]; j <= n; j += p[i])
		{
			addedge(i, SQRT, j, 0, (j - B[i]) / p[i]);
		}
		for (int j = B[i] - p[i]; j >= 1; j -= p[i])
		{
			addedge(i, SQRT, j, 0, (B[i] - j) / p[i]);
		}
	}

	dij();	// from (B[1], 0)
	if (dist[B[2]][0] >= INF) cout << "-1\n";
	else cout << dist[B[2]][0] << endl;

	return 0;
}

#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...