Submission #1147841

#TimeUsernameProblemLanguageResultExecution timeMemory
1147841Theo830Jakarta Skyscrapers (APIO15_skyscraper)C++20
100 / 100
909 ms58664 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e9+7; const ll MOD = 998244353; typedef pair<ll,ll> ii; #define iii pair<ii,ll> #define f(i,a,b) for(ll i = a;i < b;i++) #define pb push_back #define vll vector<ll> #define F first #define S second #define all(x) (x).begin(), (x).end() ///I hope I will get uprating and don't make mistakes ///I will never stop programming ///sqrt(-1) Love C++ ///Please don't hack me ///@TheofanisOrfanou Theo830 ///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst) ///Stay Calm ///Look for special cases ///Beware of overflow and array bounds ///Think the problem backwards ///Training const ll val = 200; ll dist[30005][val+5]; int main(void){ ios_base::sync_with_stdio(0); cin.tie(0); ll n,m; cin>>n>>m; ll b[m],p[m]; vector<ll>ex[n]; f(i,0,m){ cin>>b[i]>>p[i]; ex[b[i]].pb(p[i]); } ll best[n]; f(i,0,n){ best[i] = INF; f(j,0,val+5){ dist[i][j] = INF; } } best[b[0]] = 0; priority_queue<ii,vector<ii>,greater<ii> >pq,q[val+5]; pq.push(ii(0,b[0])); while(!pq.empty()){ ii f = pq.top(); pq.pop(); ll w = f.F,u = f.S; if(best[u] < w){ continue; } for(auto x:ex[u]){ if(x >= val){ ll cur = u; ll posa = 0; while(cur < n){ if(best[cur] > w + posa){ best[cur] = w + posa; pq.push(ii(best[cur],cur)); } cur += x; posa++; } cur = u; posa = 0; while(cur >= 0){ if(best[cur] > w + posa){ best[cur] = w + posa; pq.push(ii(best[cur],cur)); } cur -= x; posa++; } } else{ if(dist[u][x] > w){ dist[u][x] = w; q[x].push(ii(w,u)); } } } f(j,0,val){ while(!q[j].empty()){ ii f = q[j].top(); q[j].pop(); ll x = f.F,y = f.S; if(dist[y][j] < x){ continue; } ll pos = y - j; if(pos >= 0 && dist[pos][j] > x+1){ dist[pos][j] = x+1; q[j].push(ii(dist[pos][j],pos)); if(best[pos] > dist[pos][j]){ best[pos] = dist[pos][j]; pq.push(ii(best[pos],pos)); } } pos = y + j; if(pos < n && dist[pos][j] > x+1){ dist[pos][j] = x+1; q[j].push(ii(dist[pos][j],pos)); if(best[pos] > dist[pos][j]){ best[pos] = dist[pos][j]; pq.push(ii(best[pos],pos)); } } } } } ll ans = best[b[1]]; if(ans == INF){ ans = -1; } cout<<ans<<"\n"; }
#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...