Submission #527441

#TimeUsernameProblemLanguageResultExecution timeMemory
527441Koosha_mvJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1090 ms5176 KiB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll

const int N=50000,S=1;

int m,n,pos[N],p[N],dist[N][S];
vector<int> vec[N];
set<pair<int,pair<int,int>>> st;

main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	cin>>n>>m;
	f(i,0,m){
		cin>>pos[i]>>p[i];
		vec[pos[i]].pb(p[i]);
	}
	f(i,0,n) f(j,0,S) dist[i][j]=N;
	dist[pos[0]][0]=0;
	f(i,0,n) f(j,0,S) st.insert({dist[i][j],{i,j}});
	while(st.size()){
		int x=(*st.begin()).S.F,p=(*st.begin()).S.S;
		if(dist[x][p]==N) break;
		//cout<<endl;
		//eror(dist[2][0]);
		//cout<<x<<" "<<p<<" -> "<<dist[x][p]<<endl;
		st.erase(*st.begin());
		if(p>0){
			//eror(x+p);
			//cout<<x+p<<" "<<dist[x][p]+1<<" "<<dist[x+p][0]<<endl;
			if(x+p<n && dist[x][p]+1<dist[x+p][0]){
				//eror(100);
				st.erase({dist[x+p][0],{x+p,0}});
				dist[x+p][0]=dist[x][p]+1;
				st.insert({dist[x+p][0],{x+p,0}});
			}
			if(p<=x && dist[x][p]+1<dist[x-p][0]){
				st.erase({dist[x-p][0],{x-p,0}});
				dist[x-p][0]=dist[x][p]+1;
				st.insert({dist[x-p][0],{x-p,0}});
			}
			
			if(x+p<n && dist[x][p]+1<dist[x+p][p]){
				st.erase({dist[x+p][p],{x+p,p}});
				dist[x+p][p]=dist[x][p]+1;
				st.insert({dist[x+p][p],{x+p,p}});
			}
			if(p<=x && dist[x][p]+1<dist[x-p][p]){
				st.erase({dist[x-p][p],{x-p,p}});
				dist[x-p][p]=dist[x][p]+1;
				st.insert({dist[x-p][p],{x-p,p}});
			}
			continue ;
		}
		for(auto u : vec[x]){
			if(u<S){
				if(dist[x][u]<=dist[x][p]) continue ;
				st.erase({dist[x][u],{x,u}});
				dist[x][u]=dist[x][p];
				st.insert({dist[x][u],{x,u}});
			}
			else{
				for(int i=x%u;i<n;i+=u){
					if(dist[x][p]+abs(x-i)/u<dist[i][p]){
						st.erase({dist[i][p],{i,p}});
						dist[i][p]=dist[x][p]+abs(x-i)/u;
						st.insert({dist[i][p],{i,p}});
					}
				}
			}
		}
	}
	cout<<(dist[pos[1]][0]==N ? -1 : dist[pos[1]][0]);
}

Compilation message (stderr)

skyscraper.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | main(){
      | ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:50:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   50 |    if(x+p<n && dist[x][p]+1<dist[x+p][0]){
      |                ~~~~~~~~~^
skyscraper.cpp:53:27: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   53 |     dist[x+p][0]=dist[x][p]+1;
      |                  ~~~~~~~~~^
skyscraper.cpp:56:24: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   56 |    if(p<=x && dist[x][p]+1<dist[x-p][0]){
      |               ~~~~~~~~~^
skyscraper.cpp:58:27: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   58 |     dist[x-p][0]=dist[x][p]+1;
      |                  ~~~~~~~~~^
skyscraper.cpp:62:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   62 |    if(x+p<n && dist[x][p]+1<dist[x+p][p]){
      |                ~~~~~~~~~^
skyscraper.cpp:62:40: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   62 |    if(x+p<n && dist[x][p]+1<dist[x+p][p]){
      |                             ~~~~~~~~~~~^
skyscraper.cpp:64:27: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   64 |     dist[x+p][p]=dist[x][p]+1;
      |                  ~~~~~~~~~^
skyscraper.cpp:64:16: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   64 |     dist[x+p][p]=dist[x][p]+1;
      |     ~~~~~~~~~~~^
skyscraper.cpp:67:24: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   67 |    if(p<=x && dist[x][p]+1<dist[x-p][p]){
      |               ~~~~~~~~~^
skyscraper.cpp:67:39: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   67 |    if(p<=x && dist[x][p]+1<dist[x-p][p]){
      |                            ~~~~~~~~~~~^
skyscraper.cpp:69:27: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   69 |     dist[x-p][p]=dist[x][p]+1;
      |                  ~~~~~~~~~^
skyscraper.cpp:69:16: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
   69 |     dist[x-p][p]=dist[x][p]+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...