//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef sh short int
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast")
const int N = 3e4 + 5, M = 3e4 + 5, inf = 1e9 + 5;
int n, m, b[M], p[M], dis[M], mm = 2;
vector<int> adj[M];
vector<int> pos[N];
set<pair<int, int> > bp;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
if(i > 1)
bp.insert({b[i], p[i]});
}
for(auto x : bp)
{
bool is = 1;
if(is)
{
b[mm] = x.F;
p[mm] = x.S;
mm++;
}
}
bp.clear();
for(int i = 0; i < mm; i++)
pos[b[i]].pb(i);
for(int i = 0; i < mm; i++)
{
int ptr = b[i];
while(ptr < n)
{
for(auto x : pos[ptr])
adj[i].pb(x);
ptr += p[i];
}
ptr = b[i] - p[i];
while(ptr >= 0)
{
for(auto x : pos[ptr])
adj[i].pb(x);
ptr -= p[i];
}
}
for(int i = 1; i < mm + 5; i++)
dis[i] = inf;
set<pair<int, int> > st;
st.insert({0, 0});
while(st.size())
{
auto x = *st.begin();
//cout << x.S << endl;
st.erase(x);
for(auto v : adj[x.S])
{
int dist = abs(b[v] - b[x.S]) / p[x.S];
if(x.F + dist < dis[v])
{
dis[v] = x.F + dist;
st.insert({dis[v], v});
}
}
}
st.clear();
if(dis[1] == inf)
cout << -1 << endl;
else
cout << dis[1] << endl;
//cout << dis[1] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
2 ms |
1824 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
2 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
2 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
2 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
7 ms |
3072 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
8 ms |
2816 KB |
Output is correct |
15 |
Correct |
8 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
2 ms |
1792 KB |
Output is correct |
7 |
Correct |
2 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
8 ms |
2944 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
9 ms |
2944 KB |
Output is correct |
15 |
Correct |
8 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
2304 KB |
Output is correct |
18 |
Correct |
2 ms |
1792 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
62 ms |
18040 KB |
Output is correct |
21 |
Correct |
2 ms |
1920 KB |
Output is correct |
22 |
Correct |
2 ms |
1792 KB |
Output is correct |
23 |
Correct |
3 ms |
1792 KB |
Output is correct |
24 |
Correct |
4 ms |
2048 KB |
Output is correct |
25 |
Correct |
4 ms |
2048 KB |
Output is correct |
26 |
Correct |
2 ms |
1792 KB |
Output is correct |
27 |
Correct |
2 ms |
1920 KB |
Output is correct |
28 |
Correct |
3 ms |
2048 KB |
Output is correct |
29 |
Correct |
3 ms |
1920 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
2 ms |
1792 KB |
Output is correct |
32 |
Correct |
2 ms |
1792 KB |
Output is correct |
33 |
Correct |
8 ms |
2816 KB |
Output is correct |
34 |
Correct |
8 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1920 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1920 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
7 ms |
2944 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
8 ms |
2816 KB |
Output is correct |
15 |
Correct |
8 ms |
2944 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
6 ms |
2304 KB |
Output is correct |
18 |
Correct |
2 ms |
1792 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
60 ms |
18296 KB |
Output is correct |
21 |
Correct |
2 ms |
1792 KB |
Output is correct |
22 |
Correct |
2 ms |
1792 KB |
Output is correct |
23 |
Correct |
3 ms |
1824 KB |
Output is correct |
24 |
Correct |
4 ms |
2048 KB |
Output is correct |
25 |
Correct |
4 ms |
2048 KB |
Output is correct |
26 |
Correct |
2 ms |
1792 KB |
Output is correct |
27 |
Correct |
2 ms |
1920 KB |
Output is correct |
28 |
Correct |
3 ms |
2048 KB |
Output is correct |
29 |
Correct |
3 ms |
1920 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
2 ms |
1792 KB |
Output is correct |
32 |
Correct |
3 ms |
1792 KB |
Output is correct |
33 |
Correct |
8 ms |
2816 KB |
Output is correct |
34 |
Correct |
8 ms |
2816 KB |
Output is correct |
35 |
Correct |
113 ms |
20216 KB |
Output is correct |
36 |
Correct |
10 ms |
2688 KB |
Output is correct |
37 |
Correct |
117 ms |
20344 KB |
Output is correct |
38 |
Correct |
156 ms |
25720 KB |
Output is correct |
39 |
Correct |
148 ms |
26360 KB |
Output is correct |
40 |
Correct |
170 ms |
26488 KB |
Output is correct |
41 |
Correct |
150 ms |
25336 KB |
Output is correct |
42 |
Correct |
7 ms |
2048 KB |
Output is correct |
43 |
Correct |
7 ms |
2176 KB |
Output is correct |
44 |
Correct |
66 ms |
18168 KB |
Output is correct |
45 |
Correct |
323 ms |
50812 KB |
Output is correct |
46 |
Correct |
325 ms |
50808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1920 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
2 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
7 ms |
2944 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
8 ms |
2848 KB |
Output is correct |
15 |
Correct |
8 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
2304 KB |
Output is correct |
18 |
Correct |
2 ms |
1792 KB |
Output is correct |
19 |
Correct |
2 ms |
1920 KB |
Output is correct |
20 |
Correct |
58 ms |
18048 KB |
Output is correct |
21 |
Correct |
2 ms |
1920 KB |
Output is correct |
22 |
Correct |
2 ms |
1792 KB |
Output is correct |
23 |
Correct |
2 ms |
1920 KB |
Output is correct |
24 |
Correct |
4 ms |
2048 KB |
Output is correct |
25 |
Correct |
4 ms |
2048 KB |
Output is correct |
26 |
Correct |
2 ms |
1792 KB |
Output is correct |
27 |
Correct |
2 ms |
1920 KB |
Output is correct |
28 |
Correct |
3 ms |
2048 KB |
Output is correct |
29 |
Correct |
3 ms |
1920 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
2 ms |
1792 KB |
Output is correct |
32 |
Correct |
2 ms |
1792 KB |
Output is correct |
33 |
Correct |
8 ms |
2816 KB |
Output is correct |
34 |
Correct |
8 ms |
2816 KB |
Output is correct |
35 |
Correct |
114 ms |
20216 KB |
Output is correct |
36 |
Correct |
12 ms |
2816 KB |
Output is correct |
37 |
Correct |
118 ms |
20380 KB |
Output is correct |
38 |
Correct |
155 ms |
25592 KB |
Output is correct |
39 |
Correct |
149 ms |
26360 KB |
Output is correct |
40 |
Correct |
164 ms |
26488 KB |
Output is correct |
41 |
Correct |
150 ms |
25208 KB |
Output is correct |
42 |
Correct |
7 ms |
2048 KB |
Output is correct |
43 |
Correct |
7 ms |
2176 KB |
Output is correct |
44 |
Correct |
67 ms |
18304 KB |
Output is correct |
45 |
Correct |
331 ms |
50680 KB |
Output is correct |
46 |
Correct |
326 ms |
50808 KB |
Output is correct |
47 |
Correct |
150 ms |
17784 KB |
Output is correct |
48 |
Correct |
25 ms |
4224 KB |
Output is correct |
49 |
Correct |
20 ms |
3584 KB |
Output is correct |
50 |
Correct |
14 ms |
3072 KB |
Output is correct |
51 |
Correct |
84 ms |
8440 KB |
Output is correct |
52 |
Correct |
83 ms |
8440 KB |
Output is correct |
53 |
Correct |
38 ms |
5880 KB |
Output is correct |
54 |
Correct |
1 ms |
1920 KB |
Output is correct |
55 |
Correct |
2 ms |
1792 KB |
Output is correct |
56 |
Runtime error |
412 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Halted |
0 ms |
0 KB |
- |