#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int B[30005];
int C[30005];
int dis[30005];
bool vis[30005];
vector<int> V[30005];
typedef pair<int,int> P;
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
clock_t st = clock();
int i, j;
for(i=0;i<M;i++) cin >> B[i] >> C[i];
for(i=0;i<N;i++) dis[i] = INF;
for(i=0;i<M;i++) V[B[i]].push_back(i);
dis[B[0]] = 0;
priority_queue<P,vector<P>,greater<P>> PQ;
PQ.push(P(0,B[0]));
while(!PQ.empty()) {
if(clock() - st >= 990000) break;
P k2 = PQ.top();
PQ.pop();
int id = k2.second;
if(vis[id]) continue;
vis[id] = true;
set<int> S;
for(int k : V[id]) {
if(S.find(C[k])!=S.end()) continue;
S.insert(C[k]);
int st = B[k] % C[k];
assert(B[k]==id);
for(i=st;i<N;i+=C[k]) {
int d = abs(i - B[k]) / C[k];
if(dis[i] > dis[id] + d) {
dis[i] = dis[id] + d;
PQ.push(P(dis[i], i));
}
}
}
}
cout << (dis[B[1]]==INF?-1:dis[B[1]]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:17:12: warning: unused variable 'j' [-Wunused-variable]
17 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1032 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
948 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
1080 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
28 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
4 ms |
1180 KB |
Output is correct |
32 |
Correct |
3 ms |
1168 KB |
Output is correct |
33 |
Correct |
8 ms |
1236 KB |
Output is correct |
34 |
Correct |
7 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
26 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
3 ms |
1108 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
7 ms |
1236 KB |
Output is correct |
34 |
Correct |
7 ms |
1236 KB |
Output is correct |
35 |
Correct |
9 ms |
1540 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
9 ms |
1424 KB |
Output is correct |
38 |
Correct |
12 ms |
1620 KB |
Output is correct |
39 |
Correct |
11 ms |
1616 KB |
Output is correct |
40 |
Correct |
12 ms |
1624 KB |
Output is correct |
41 |
Correct |
12 ms |
1620 KB |
Output is correct |
42 |
Correct |
9 ms |
1492 KB |
Output is correct |
43 |
Correct |
7 ms |
1492 KB |
Output is correct |
44 |
Correct |
31 ms |
1508 KB |
Output is correct |
45 |
Correct |
15 ms |
1620 KB |
Output is correct |
46 |
Correct |
15 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1084 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
26 ms |
1164 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
4 ms |
1108 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
8 ms |
1236 KB |
Output is correct |
34 |
Correct |
7 ms |
1236 KB |
Output is correct |
35 |
Correct |
9 ms |
1540 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
9 ms |
1492 KB |
Output is correct |
38 |
Correct |
11 ms |
1492 KB |
Output is correct |
39 |
Correct |
11 ms |
1492 KB |
Output is correct |
40 |
Correct |
12 ms |
1628 KB |
Output is correct |
41 |
Correct |
12 ms |
1492 KB |
Output is correct |
42 |
Correct |
5 ms |
1492 KB |
Output is correct |
43 |
Correct |
5 ms |
1628 KB |
Output is correct |
44 |
Correct |
30 ms |
1516 KB |
Output is correct |
45 |
Correct |
17 ms |
1620 KB |
Output is correct |
46 |
Correct |
14 ms |
1636 KB |
Output is correct |
47 |
Correct |
40 ms |
2256 KB |
Output is correct |
48 |
Correct |
5 ms |
1688 KB |
Output is correct |
49 |
Correct |
5 ms |
1748 KB |
Output is correct |
50 |
Correct |
4 ms |
1620 KB |
Output is correct |
51 |
Correct |
56 ms |
3148 KB |
Output is correct |
52 |
Correct |
60 ms |
2640 KB |
Output is correct |
53 |
Correct |
27 ms |
2544 KB |
Output is correct |
54 |
Correct |
6 ms |
1408 KB |
Output is correct |
55 |
Correct |
8 ms |
1412 KB |
Output is correct |
56 |
Correct |
992 ms |
2692 KB |
Output is correct |
57 |
Correct |
168 ms |
3400 KB |
Output is correct |
58 |
Correct |
16 ms |
1940 KB |
Output is correct |
59 |
Correct |
18 ms |
2068 KB |
Output is correct |
60 |
Correct |
19 ms |
2004 KB |
Output is correct |
61 |
Correct |
18 ms |
2076 KB |
Output is correct |
62 |
Correct |
24 ms |
2584 KB |
Output is correct |
63 |
Correct |
81 ms |
2952 KB |
Output is correct |
64 |
Correct |
83 ms |
3980 KB |
Output is correct |
65 |
Correct |
103 ms |
3912 KB |
Output is correct |
66 |
Correct |
153 ms |
3960 KB |
Output is correct |
67 |
Correct |
175 ms |
3952 KB |
Output is correct |