제출 #410304

#제출 시각아이디문제언어결과실행 시간메모리
410304jeqchoJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
849 ms24460 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<pair<int,int>> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define sz(x) int(x.size()) #define all(x) begin(x), end(x) #define fi first #define se second struct triple { int a,b,c; }; bool operator<(const triple &x, const triple &y) { return x.a<y.a; } int const N=3e4+3; int const sN=174+3; int const M=3e4+3; int b[M]; int p[M]; int d[N][sN]; bitset<sN> vis[N]; int const INF=1e9; vi doges[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; F0R(i,m) { cin>>b[i]>>p[i]; doges[b[i]].pb(i); } F0R(i,n) { fill(d[i],d[i]+sN,INF); } priority_queue<triple> q; q.push({0,b[0],0}); d[b[0]][0]=0; while(!q.empty()&&(!vis[b[1]][0])) { int pos = q.top().b; int pw = q.top().c; q.pop(); if(vis[pos][pw])continue; vis[pos][pw]=1; if(pw==0) { trav(doge,doges[pos]) { if(p[doge]>=sN) { int nxt=pos+p[doge]; int cnt=1; while(nxt<n) { int cand = d[pos][0]+cnt; if(cand<d[nxt][0]) { d[nxt][0]=cand; q.push({-cand,nxt,0}); } ++cnt; nxt+=p[doge]; } nxt=pos-p[doge]; cnt=1; while(nxt>=0) { int cand = d[pos][0]+cnt; if(cand<d[nxt][0]) { d[nxt][0]=cand; q.push({-cand,nxt,0}); } ++cnt; nxt-=p[doge]; } } else { if(vis[pos][p[doge]])continue; d[pos][p[doge]]=d[pos][0]; q.push({1,pos,p[doge]}); } } } else { if(!vis[pos][0]) { d[pos][0]=d[pos][pw]; q.push({1,pos,0}); } int cand = d[pos][pw]+1; int nxt = pos+pw; if(nxt<n && cand<d[nxt][pw]) { d[nxt][pw]=cand; q.push({-cand,nxt,pw}); } nxt = pos-pw; if(nxt>=0 && cand<d[nxt][pw]) { d[nxt][pw]=cand; q.push({-cand,nxt,pw}); } } } if(d[b[1]][0]==INF) { cout<<-1<<'\n'; } else { cout<<d[b[1]][0]<<'\n'; } 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...