# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
116504 | roseanne_pcy | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 3e4+5;
const int half = 173;
int n, m;
int dist[maxn*180*2];
vector<int> adj[maxn*180*2];
bool ok(int x)
{
return 0<= x && x< n;
}
int N;
void bfs(int s)
{
for(int i = 0; i< N+10; i++) dist[i] = 1e9;
queue<int> q;
dist[s] = 0; q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int v : adj[u])
{
if(dist[v]> dist[u]+1)
{
dist[v] = dist[u]+1;
q.push(v);
}
}
}
}
int hom[maxn], mana[maxn];
int conv(int u, int md, int isleft)
{
if(isleft) return (half+1)*n+(md-1)*n+u;
return md*n+u;
}
int main()
{
scanf("%d %d", &n, &m);
N = conv(n-1, md, 1);
for(int i = 0; i< m; i++)
{
scanf("%d %d", &hom[i], &mana[i]);
}
// printf("okay\n");
for(int i = 0; i< n; i++)
{
for(int md = 1; md<= half; md++)
{
if(ok(i+md))
{
adj[conv(i, md, 0)].pb(i+md);
adj[conv(i, md, 0)].pb(conv(i+md, md, 0));
}
if(ok(i-md))
{
adj[conv(i, md, 1)].pb(i-md);
adj[conv(i, md, 1)].pb(conv(i-md, md, 1));
}
}
}
for(int i = 0; i< m; i++)
{
int pw = mana[i];
if(pw> half)
{
int cur = hom[i];
while(cur< n)
{
int nxt = cur+pw;
if(ok(nxt)) adj[cur].pb(nxt);
cur = nxt;
}
cur = hom[i];
while(cur>= 0)
{
int nxt = cur-pw;
if(ok(nxt)) adj[cur].pb(nxt);
cur = nxt;
}
}
else
{
int x = hom[i];
if(ok(x-pw))
{
adj[x].pb(conv(x-pw, pw, 1));
adj[x].pb(x-pw);
}
if(ok(x+pw))
{
adj[x].pb(conv(x+pw, pw, 0));
adj[x].pb(x+pw);
}
}
}
bfs(hom[0]);
if(dist[hom[1]] == 1e9) printf("-1\n");
else printf("%d\n", dist[hom[1]]);
}