Submission #388824

#TimeUsernameProblemLanguageResultExecution timeMemory
388824talant117408Jakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
936 ms262148 KiB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 998244353;
 
ll mode(ll a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}
 
const int N = 3e4+7;
int dist[N], who[N];
vector <pii> graph[N];
 
int main() {
    do_not_disturb
    
    int h, n;
    cin >> h >> n;
    vector <pii> doges(n);
    for (auto &to : doges) cin >> to.first >> to.second;
    for (int i = 0; i < n; i++) {
		who[doges[i].first]++;
	}
	set <int> powers[h];
	for (int i = 0; i < n; i++) {
		powers[doges[i].first].insert(doges[i].second);
	}
	
	for (int i = 0; i < h; i++) {
		
		if (sz(powers[i]) == 0) continue;
		
		vector <int> ps;
		for (auto to : powers[i]) ps.pb(to);
		reverse(all(ps));
		
		for (auto jump : ps) {
			int cnt = 0;
			for (int pos = i; pos < h; pos += jump) {
              	auto it = lb(all(graph[i]), mp(pos, 0));
				if (who[pos] && pos != i && (it == graph[i].end() || (*it).first != pos)) {
					graph[i].pb(mp(pos, cnt));
				}
				cnt++;
			}
			cnt = 0;
			for (int pos = i; pos >= 0; pos -= jump) {
              	auto it = lb(all(graph[i]), mp(pos, 0));
				if (who[pos] && pos != i && (it == graph[i].end() || (*it).first != pos)) {
					graph[i].pb(mp(pos, cnt));
				}
				cnt++;
			}
		}
	}
	
	for (int i = 0; i < h; i++) {
		dist[i] = 1e9;
	}
	dist[doges[0].first] = 0;
	priority_queue <pii> K;
	K.push(mp(0, doges[0].first));
	
	while(!K.empty()) {
		auto v = K.top().second; K.pop();
		for (auto to : graph[v]) {
			if (dist[to.first] > dist[v]+to.second) {
				dist[to.first] = dist[v]+to.second;
				K.push(mp(-dist[to.first], to.first));
			}
		}
	}
	
	if (dist[doges[1].first] >= 1e9) cout << -1;
	else cout << dist[doges[1].first];
    
    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...