#include <bits/stdc++.h>
using namespace std;
const int oo = INT_MAX;
const int nmax = 3e4;
const int bsize = 173;
int n,m;
int b[nmax + 5], p[nmax + 5];
int r[nmax + 5][bsize + 5];
int d[nmax + 5], d_l[nmax + 5][bsize + 5];
bool sel[nmax + 5], sel_l[nmax + 5][bsize + 5];
vector<int> l[nmax + 5];
typedef pair<int,pair<int,int>> pi;
priority_queue <pi,vector<pi>,greater<pi>> h;
void switch_doge(int t, int poz)
{
sel[poz] = true;
d[poz] = t;
for(auto ln : l[poz])
{
if(ln < bsize)
{
if(poz - ln >= 0 && t + 1 < d_l[poz - ln][ln])
{
d_l[poz - ln][ln] = t + 1;
h.push({t + 1, {poz - ln, ln}});
}
if(poz + ln < n && t + 1 < d_l[poz + ln][ln])
{
d_l[poz + ln][ln] = t + 1;
h.push({t + 1, {poz + ln, ln}});
}
}
else
{
for(int i=poz;i>=0;i-=ln)
{
int t_aux = t + (poz - i) / ln;
if(t_aux < d[i])
{
d[i] = t_aux;
h.push({t_aux,{i,ln}});
}
}
for(int i=poz+ln;i<n;i+=ln)
{
int t_aux = t + (i - poz) / ln;
if(t_aux < d[i])
{
d[i] = t_aux;
h.push({t_aux,{i,ln}});
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n>>m;
for(int i=0; i<n; i++)
{
d[i] = oo;
for(int ln=1; ln<bsize; ln++)
{
r[i][ln] = oo;
d_l[i][ln] = oo;
}
}
for(int i=0; i<m; i++)
{
cin>>b[i]>>p[i];
l[b[i]].push_back(p[i]);
}
h.push({0,{b[0],p[0]}});
if(b[0] < bsize)
{
d_l[b[0]][p[0]] = 0;
}
while(!h.empty())
{
while(!h.empty())
{
int poz = h.top().second.first;
int ln = h.top().second.second;
if(ln < bsize)
{
if(sel_l[poz][ln])
{
h.pop();
continue;
}
else
{
break;
}
}
else
{
if(sel[poz])
{
h.pop();
continue;
}
else
{
break;
}
}
}
if(h.empty())
{
break;
}
int t = h.top().first;
int poz = h.top().second.first;
int ln = h.top().second.second;
h.pop();
if(!sel[poz])
{
switch_doge(t,poz);
}
if(ln < bsize)
{
sel_l[poz][ln] = true;
if(poz - ln >= 0 && t + 1 < d_l[poz - ln][ln])
{
d_l[poz - ln][ln] = 1 + t;
h.push({1 + t, {poz - ln, ln}});
}
if(poz + ln < n && t + 1 < d_l[poz + ln][ln])
{
d_l[poz + ln][ln] = 1 + t;
h.push({1 + t, {poz + ln, ln}});
}
}
}
int rez = d[b[1]];
if(rez==oo)
{
cout<<-1<<'\n';
return 0;
}
cout<<rez<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1044 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 |
1040 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 |
1036 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
2 ms |
1236 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1148 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
1044 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 |
1040 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1040 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1304 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
3 ms |
2328 KB |
Output is correct |
18 |
Correct |
2 ms |
3480 KB |
Output is correct |
19 |
Correct |
2 ms |
3860 KB |
Output is correct |
20 |
Correct |
2 ms |
4252 KB |
Output is correct |
21 |
Correct |
1 ms |
1620 KB |
Output is correct |
22 |
Correct |
2 ms |
3540 KB |
Output is correct |
23 |
Correct |
3 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
4124 KB |
Output is correct |
25 |
Correct |
4 ms |
4180 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
3 ms |
4252 KB |
Output is correct |
29 |
Correct |
8 ms |
4276 KB |
Output is correct |
30 |
Correct |
3 ms |
4120 KB |
Output is correct |
31 |
Correct |
5 ms |
4180 KB |
Output is correct |
32 |
Correct |
4 ms |
4176 KB |
Output is correct |
33 |
Correct |
13 ms |
4336 KB |
Output is correct |
34 |
Correct |
17 ms |
4288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1032 KB |
Output is correct |
3 |
Correct |
1 ms |
1044 KB |
Output is correct |
4 |
Correct |
1 ms |
1040 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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1044 KB |
Output is correct |
10 |
Correct |
1 ms |
1164 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1172 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1300 KB |
Output is correct |
17 |
Correct |
4 ms |
2260 KB |
Output is correct |
18 |
Correct |
3 ms |
3476 KB |
Output is correct |
19 |
Correct |
3 ms |
3796 KB |
Output is correct |
20 |
Correct |
3 ms |
4248 KB |
Output is correct |
21 |
Correct |
1 ms |
1620 KB |
Output is correct |
22 |
Correct |
2 ms |
3540 KB |
Output is correct |
23 |
Correct |
2 ms |
3604 KB |
Output is correct |
24 |
Correct |
4 ms |
4124 KB |
Output is correct |
25 |
Correct |
3 ms |
4252 KB |
Output is correct |
26 |
Correct |
2 ms |
4248 KB |
Output is correct |
27 |
Correct |
3 ms |
4252 KB |
Output is correct |
28 |
Correct |
3 ms |
4248 KB |
Output is correct |
29 |
Correct |
10 ms |
4280 KB |
Output is correct |
30 |
Correct |
3 ms |
4180 KB |
Output is correct |
31 |
Correct |
6 ms |
4180 KB |
Output is correct |
32 |
Correct |
4 ms |
4180 KB |
Output is correct |
33 |
Correct |
17 ms |
4356 KB |
Output is correct |
34 |
Correct |
16 ms |
4308 KB |
Output is correct |
35 |
Correct |
11 ms |
4020 KB |
Output is correct |
36 |
Correct |
3 ms |
2844 KB |
Output is correct |
37 |
Correct |
23 ms |
4820 KB |
Output is correct |
38 |
Correct |
16 ms |
5020 KB |
Output is correct |
39 |
Correct |
17 ms |
5020 KB |
Output is correct |
40 |
Correct |
16 ms |
5076 KB |
Output is correct |
41 |
Correct |
16 ms |
5076 KB |
Output is correct |
42 |
Correct |
6 ms |
4836 KB |
Output is correct |
43 |
Correct |
5 ms |
4816 KB |
Output is correct |
44 |
Correct |
5 ms |
4820 KB |
Output is correct |
45 |
Correct |
48 ms |
5340 KB |
Output is correct |
46 |
Correct |
46 ms |
5328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1036 KB |
Output is correct |
2 |
Correct |
1 ms |
1036 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1044 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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1300 KB |
Output is correct |
12 |
Correct |
1 ms |
1180 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
3 ms |
2328 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
2 ms |
3864 KB |
Output is correct |
20 |
Correct |
3 ms |
4308 KB |
Output is correct |
21 |
Correct |
2 ms |
1620 KB |
Output is correct |
22 |
Correct |
3 ms |
3592 KB |
Output is correct |
23 |
Correct |
2 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
4140 KB |
Output is correct |
25 |
Correct |
4 ms |
4180 KB |
Output is correct |
26 |
Correct |
3 ms |
4252 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
7 ms |
4180 KB |
Output is correct |
30 |
Correct |
5 ms |
4180 KB |
Output is correct |
31 |
Correct |
5 ms |
4244 KB |
Output is correct |
32 |
Correct |
5 ms |
4236 KB |
Output is correct |
33 |
Correct |
13 ms |
4288 KB |
Output is correct |
34 |
Correct |
14 ms |
4308 KB |
Output is correct |
35 |
Correct |
13 ms |
4136 KB |
Output is correct |
36 |
Correct |
4 ms |
2900 KB |
Output is correct |
37 |
Correct |
17 ms |
4860 KB |
Output is correct |
38 |
Correct |
22 ms |
5076 KB |
Output is correct |
39 |
Correct |
23 ms |
5016 KB |
Output is correct |
40 |
Correct |
17 ms |
5076 KB |
Output is correct |
41 |
Correct |
17 ms |
5084 KB |
Output is correct |
42 |
Correct |
5 ms |
4892 KB |
Output is correct |
43 |
Correct |
6 ms |
4820 KB |
Output is correct |
44 |
Correct |
6 ms |
4840 KB |
Output is correct |
45 |
Correct |
45 ms |
5320 KB |
Output is correct |
46 |
Correct |
52 ms |
5292 KB |
Output is correct |
47 |
Correct |
84 ms |
21576 KB |
Output is correct |
48 |
Correct |
18 ms |
34352 KB |
Output is correct |
49 |
Correct |
19 ms |
37372 KB |
Output is correct |
50 |
Correct |
26 ms |
40660 KB |
Output is correct |
51 |
Correct |
58 ms |
49640 KB |
Output is correct |
52 |
Correct |
60 ms |
49644 KB |
Output is correct |
53 |
Correct |
31 ms |
49300 KB |
Output is correct |
54 |
Correct |
26 ms |
48088 KB |
Output is correct |
55 |
Correct |
22 ms |
48144 KB |
Output is correct |
56 |
Correct |
25 ms |
49552 KB |
Output is correct |
57 |
Correct |
71 ms |
48256 KB |
Output is correct |
58 |
Correct |
23 ms |
48812 KB |
Output is correct |
59 |
Correct |
25 ms |
48796 KB |
Output is correct |
60 |
Correct |
32 ms |
48796 KB |
Output is correct |
61 |
Correct |
34 ms |
48932 KB |
Output is correct |
62 |
Correct |
43 ms |
49692 KB |
Output is correct |
63 |
Correct |
70 ms |
50180 KB |
Output is correct |
64 |
Correct |
57 ms |
50244 KB |
Output is correct |
65 |
Correct |
288 ms |
50348 KB |
Output is correct |
66 |
Correct |
684 ms |
50620 KB |
Output is correct |
67 |
Correct |
784 ms |
50648 KB |
Output is correct |