# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
105292 | DystoriaX | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1073 ms | 30372 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>
#define eb emplace_back
using namespace std;
int n, m;
int b[30010], p[30010];
vector<int> doge[30010];
const int sq = sqrt(30000) + 3;
const int INF = 1e9;
int dist[sq + 1][30010];
priority_queue<tuple<int, int, int> > pq;
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d%d", &b[i], &p[i]);
doge[b[i]].eb(p[i]);
}
for(int i = 0; i < n; i++)
for(int j = 0; j <= sq; j++)
dist[j][i] = INF;
dist[p[0] > sq ? 0 : p[0]][b[0]] = 0;
pq.push({0, b[0], p[0]});
while(!pq.empty()){
int d, pos, pow;
tie(d, pos, pow) = pq.top(); pq.pop();
d = -d;
if(d != dist[pow > sq ? 0 : pow][pos]) continue;
if(pow != 0) doge[pos].eb(pow);
for(auto v : doge[pos]){
int pow = v;
if(pow <= sq){
if(pos + pow < n && dist[pow][pos + pow] > d + 1){
dist[pow][pos + pow] = d + 1;
pq.push({-dist[pow][pos + pow], pos + pow, pow});
}
if(pos - pow >= 0 && dist[pow][pos - pow] > d + 1){
dist[pow][pos - pow] = d + 1;
pq.push({-dist[pow][pos - pow], pos - pow, pow});
}
} else {
int k = 1;
while(pos + k * pow < n){
if(dist[0][pos + k * pow] > d + k){
dist[0][pos + k * pow] = d + k;
pq.push({-dist[0][pos + k * pow], pos + k * pow, 0});
}
k++;
}
k = 1;
while(pos - k * pow >= 0){
if(dist[0][pos - k * pow] > d + k){
dist[0][pos - k * pow] = d + k;
pq.push({-dist[0][pos - k * pow], pos - k * pow, 0});
}
k++;
}
}
}
}
int ans = INF;
for(int i = 0; i <= sq; i++) ans = min(ans, dist[i][b[1]]);
printf("%d\n", ans == INF ? -1 : ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |