#include<bits/stdc++.h>
#define pb push_back
#define ins insert
#define F first
#define S second
#define var auto
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int Max = 30002;
const int Sq = 200;
const int SQ = 2*Sq+1;
const int INF = 1000000010;
vector<int> pws[Max];
vector<int> t2[Max];
vector<int> t1[Max];
int dist[Max * SQ];
int n , m;
#define index(a , b) ((a)*SQ + (b))
void Find(int v)
{
deque<int> q;q.pb(v);dist[v] = 0;
while(q.size())
{
v = q.front();
q.pop_front();
int x = v / SQ;
int y = v - x * SQ;
//if(y <= Sq)
// cout << "(" << x << "," << y << ") = " << dist[v] << "\n";
if(y == 0)
{
//yal haye 0
for(int z : t1[x])
{
if(dist[index(x , z)] > dist[v])
{
dist[index(x , z)] = dist[v];
q.push_front(index(x , z));
}
}
//yal haye 1
if(dist[index(x , Sq+1)] > dist[v] + 1)
{
dist[index(x , Sq+1)] = dist[v] + 1;
q.pb(index(x , Sq+1));
}
}
else if(y <= Sq)
{
//yal haye 0
if(dist[index(x , 0)] > dist[v])
{
dist[index(x , 0)] = dist[v];
q.push_front(index(x , 0));
}
//yal haye 1
if(x + y < n)
if(dist[index(x + y , y)] > dist[v] + 1)
{
dist[index(x + y , y)] = dist[v] + 1;
q.pb(index(x + y , y));
}
if(x - y >= 0)
if(dist[index(x - y , y)] > dist[v] + 1)
{
dist[index(x - y , y)] = dist[v] + 1;
q.pb(index(x - y , y));
}
}
else
{
y -= Sq;
//yal haye 0
for(int z : t2[x])
{
if(x + y * z >= n)
break;
if(dist[index(x + y * z, 0)] > dist[v])
{
dist[index(x + y * z , 0)] = dist[v];
q.push_front(index(x + y * z , 0));
}
}
for(int z : t2[x])
{
if(x - y * z < 0)
break;
if(dist[index(x - y * z, 0)] > dist[v])
{
dist[index(x - y * z , 0)] = dist[v];
q.push_front(index(x - y * z , 0));
}
}
//yal haye 1
if(y != Sq)
if(dist[v + 1] > dist[v] + 1)
{
dist[v + 1] = dist[v] + 1;
q.pb(v + 1);
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
int b0 = -1;
int b1 = -1;
for(int i = 0; i < m ; i++)
{
int b , p;cin >> b >> p;
pws[b].pb(p);
if(i == 0)
b0 = b;
if(i == 1)
b1 = b;
}
for(int i = 0; i < n ; i++)
{
for(int j = 0; j < SQ; j++)
dist[index(i , j)] = INF;
sort(pws[i].begin() , pws[i].end());
pws[i].resize(unique(pws[i].begin() , pws[i].end()) - pws[i].begin());
for(int x : pws[i])
{
if(x <= Sq)
t1[i].pb(x);
else
t2[i].pb(x);
}
}
Find(index(b0 , 0));
if(dist[index(b1 , 0)] < INF)
cout << dist[index(b1 , 0)] << '\n';
else
cout << -1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2816 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
3712 KB |
Output is correct |
18 |
Correct |
4 ms |
5376 KB |
Output is correct |
19 |
Correct |
4 ms |
5760 KB |
Output is correct |
20 |
Correct |
8 ms |
5760 KB |
Output is correct |
21 |
Correct |
3 ms |
3072 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
10 ms |
4992 KB |
Output is correct |
24 |
Correct |
12 ms |
5632 KB |
Output is correct |
25 |
Correct |
12 ms |
5632 KB |
Output is correct |
26 |
Correct |
8 ms |
5632 KB |
Output is correct |
27 |
Correct |
8 ms |
5632 KB |
Output is correct |
28 |
Correct |
10 ms |
5760 KB |
Output is correct |
29 |
Correct |
9 ms |
5632 KB |
Output is correct |
30 |
Correct |
9 ms |
5632 KB |
Output is correct |
31 |
Correct |
9 ms |
5632 KB |
Output is correct |
32 |
Correct |
9 ms |
5632 KB |
Output is correct |
33 |
Correct |
11 ms |
5632 KB |
Output is correct |
34 |
Correct |
11 ms |
5632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
3712 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5632 KB |
Output is correct |
20 |
Correct |
10 ms |
5760 KB |
Output is correct |
21 |
Correct |
3 ms |
3072 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
9 ms |
4992 KB |
Output is correct |
24 |
Correct |
12 ms |
5504 KB |
Output is correct |
25 |
Correct |
12 ms |
5632 KB |
Output is correct |
26 |
Correct |
8 ms |
5632 KB |
Output is correct |
27 |
Correct |
8 ms |
5632 KB |
Output is correct |
28 |
Correct |
13 ms |
5888 KB |
Output is correct |
29 |
Correct |
9 ms |
5632 KB |
Output is correct |
30 |
Correct |
8 ms |
5632 KB |
Output is correct |
31 |
Correct |
9 ms |
5760 KB |
Output is correct |
32 |
Correct |
9 ms |
5632 KB |
Output is correct |
33 |
Correct |
10 ms |
5632 KB |
Output is correct |
34 |
Correct |
11 ms |
5632 KB |
Output is correct |
35 |
Correct |
17 ms |
5120 KB |
Output is correct |
36 |
Correct |
8 ms |
4224 KB |
Output is correct |
37 |
Correct |
20 ms |
5888 KB |
Output is correct |
38 |
Correct |
22 ms |
6016 KB |
Output is correct |
39 |
Correct |
22 ms |
6144 KB |
Output is correct |
40 |
Correct |
23 ms |
6016 KB |
Output is correct |
41 |
Correct |
22 ms |
6144 KB |
Output is correct |
42 |
Correct |
13 ms |
5888 KB |
Output is correct |
43 |
Correct |
13 ms |
5888 KB |
Output is correct |
44 |
Correct |
12 ms |
5888 KB |
Output is correct |
45 |
Correct |
21 ms |
6016 KB |
Output is correct |
46 |
Correct |
21 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
2 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
2 ms |
2464 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2944 KB |
Output is correct |
17 |
Correct |
6 ms |
3712 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5632 KB |
Output is correct |
20 |
Correct |
10 ms |
5760 KB |
Output is correct |
21 |
Correct |
3 ms |
3072 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
9 ms |
4992 KB |
Output is correct |
24 |
Correct |
12 ms |
5504 KB |
Output is correct |
25 |
Correct |
12 ms |
5632 KB |
Output is correct |
26 |
Correct |
8 ms |
5632 KB |
Output is correct |
27 |
Correct |
8 ms |
5632 KB |
Output is correct |
28 |
Correct |
10 ms |
5760 KB |
Output is correct |
29 |
Correct |
9 ms |
5632 KB |
Output is correct |
30 |
Correct |
8 ms |
5632 KB |
Output is correct |
31 |
Correct |
11 ms |
5632 KB |
Output is correct |
32 |
Correct |
9 ms |
5632 KB |
Output is correct |
33 |
Correct |
11 ms |
5760 KB |
Output is correct |
34 |
Correct |
11 ms |
5632 KB |
Output is correct |
35 |
Correct |
17 ms |
5120 KB |
Output is correct |
36 |
Correct |
8 ms |
4224 KB |
Output is correct |
37 |
Correct |
22 ms |
5888 KB |
Output is correct |
38 |
Correct |
22 ms |
6016 KB |
Output is correct |
39 |
Correct |
22 ms |
6016 KB |
Output is correct |
40 |
Correct |
23 ms |
6016 KB |
Output is correct |
41 |
Correct |
23 ms |
6016 KB |
Output is correct |
42 |
Correct |
12 ms |
5888 KB |
Output is correct |
43 |
Correct |
13 ms |
5888 KB |
Output is correct |
44 |
Correct |
12 ms |
5888 KB |
Output is correct |
45 |
Correct |
21 ms |
6144 KB |
Output is correct |
46 |
Correct |
21 ms |
6016 KB |
Output is correct |
47 |
Correct |
146 ms |
22656 KB |
Output is correct |
48 |
Correct |
33 ms |
39800 KB |
Output is correct |
49 |
Correct |
35 ms |
43136 KB |
Output is correct |
50 |
Correct |
38 ms |
46968 KB |
Output is correct |
51 |
Correct |
450 ms |
50940 KB |
Output is correct |
52 |
Correct |
470 ms |
51036 KB |
Output is correct |
53 |
Correct |
146 ms |
50680 KB |
Output is correct |
54 |
Correct |
59 ms |
49536 KB |
Output is correct |
55 |
Correct |
74 ms |
49536 KB |
Output is correct |
56 |
Correct |
95 ms |
51452 KB |
Output is correct |
57 |
Correct |
224 ms |
49664 KB |
Output is correct |
58 |
Correct |
98 ms |
49784 KB |
Output is correct |
59 |
Correct |
105 ms |
49784 KB |
Output is correct |
60 |
Correct |
110 ms |
49912 KB |
Output is correct |
61 |
Correct |
110 ms |
49784 KB |
Output is correct |
62 |
Correct |
160 ms |
51448 KB |
Output is correct |
63 |
Correct |
364 ms |
50040 KB |
Output is correct |
64 |
Correct |
364 ms |
50164 KB |
Output is correct |
65 |
Correct |
520 ms |
50168 KB |
Output is correct |
66 |
Correct |
684 ms |
50296 KB |
Output is correct |
67 |
Correct |
677 ms |
50168 KB |
Output is correct |