Submission #390282

#TimeUsernameProblemLanguageResultExecution timeMemory
390282CSQ31Jakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
110 ms6552 KiB
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int sq = 300;
vector<vector<bool>>has(30001,vector<bool>(300));
vii power(30001);
vector<int>dist(30001,1e9);
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<int>b(m),p(m);
	for(int i=0;i<m;i++){
		cin>>b[i]>>p[i];
		if(p[i] < sq){
			if(!has[b[i]][p[i]])power[b[i]].pb(p[i]);
			has[b[i]][p[i]] = 1;
		}else{
			power[b[i]].pb(p[i]);
		}
	}
	dist[b[0]] = 0;
	priority_queue<pii,vector<pii>,greater<pii>>pq;
	pq.push({0,b[0]});
	while(!pq.empty()){
		int v = pq.top().se;
		int d = pq.top().fi;
		pq.pop();
		if(d != dist[v])continue;
		if(v == b[1]){
			cout<<d;
			return 0;
		}
		//cout<<v<<" "<<d<<'\n';
		for(int p:power[v]){
			for(int x=v+p;x<n;x+=p){
				if(d + (x-v)/p < dist[x]){
					dist[x] = d + (x-v)/p;
					pq.push({dist[x],x});
				} 
				if(p < sq && has[x][p])break; //has power no need to look further
			} 
			for(int x=v-p;x>=0;x-=p){
				if(d + (v-x)/p < dist[x]){
					dist[x] = d + (v-x)/p;
					pq.push({dist[x],x});
				} 
				if(p < sq && has[x][p])break; //has power no need to look further
			} 
		}
		
	}
	cout<<-1;

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