#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=3e4+100;
const ll inf=1e9+7;
const ll mod=1e9+7;
ll n;
ll dis[maxN];
priority_queue<pli,vector<pli>,greater<pli>>pq;
vector<ll>lon[maxN];
vector<pli>be[maxN];
ll p[maxN],b[maxN];
const ll block=175;
vector<ll>vec[block+2][block+2];
ll m;
void dij(ll s)
{
for(int i=0;i<n;i++) dis[i]=inf;
dis[s]=0;
pq.push({dis[s],s});
while(!pq.empty())
{
ll u=pq.top().se;
ll d=pq.top().fi;
pq.pop();
if(d>dis[u]) continue;
if(u==b[2])
{
break;
}
for(auto i:lon[u])
{
for(int j=u-p[i];j>=0;j-=p[i])
{
if(dis[j]>dis[u]+(u-j)/p[i])
{
dis[j]=dis[u]+(u-j)/p[i];
pq.push({dis[j],j});
}
}
for(int j=u+p[i];j<n;j+=p[i])
{
if(dis[j]>dis[u]+(j-u)/p[i])
{
dis[j]=dis[u]+(j-u)/p[i];
pq.push({dis[j],j});
}
}
}
for(auto cc:be[u])
{
ll i=cc.fi;
ll j=u%i;
ll k=cc.se;
ll pos=vec[i][j][k];
ll pos2=k>0?vec[i][j][k-1]:0;
for(int q=pos-i;q>=pos2;q-=i)
{
if(dis[q]>dis[pos]+(pos-q)/i)
{
dis[q]=dis[pos]+(pos-q)/i;
pq.push({dis[q],q});
}
}
pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
for(int q=pos+i;q<=pos2;q+=i)
{
if(dis[q]>dis[pos]+(q-pos)/i)
{
dis[q]=dis[pos]+(q-pos)/i;
pq.push({dis[q],q});
}
}
}
}
}
void solve()
{
cin >> n >> m;
for(int i=1;i<=m;i++)
{
cin >> b[i] >> p[i];
}
for(int i=1;i<=m;i++)
{
if(p[i]>block)
{
lon[b[i]].pb(i);
}
else
{
vec[p[i]][b[i]%p[i]].pb(b[i]);
}
}
for(int i=1;i<=block;i++)
{
for(int j=0;j<i;j++)
{
sort(vec[i][j].begin(),vec[i][j].end());
for(int k=0;k<vec[i][j].size();k++)
{
be[vec[i][j][k]].pb({i,k});
}
}
}
dij(b[1]);
if(dis[b[2]]==inf) cout << -1<<'\n';
else cout << dis[b[2]];
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
skyscraper.cpp: In function 'void dij(ll)':
skyscraper.cpp:71:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
| ~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int k=0;k<vec[i][j].size();k++)
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2432 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
2 ms |
2492 KB |
Output is correct |
18 |
Correct |
2 ms |
2436 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2488 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
2 ms |
2516 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2496 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2608 KB |
Output is correct |
31 |
Correct |
3 ms |
2664 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
4 ms |
2900 KB |
Output is correct |
34 |
Correct |
3 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
2 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2488 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2496 KB |
Output is correct |
24 |
Correct |
3 ms |
2516 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Correct |
2 ms |
2492 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2516 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
4 ms |
2900 KB |
Output is correct |
34 |
Correct |
4 ms |
2900 KB |
Output is correct |
35 |
Correct |
6 ms |
3156 KB |
Output is correct |
36 |
Correct |
2 ms |
2624 KB |
Output is correct |
37 |
Correct |
5 ms |
3128 KB |
Output is correct |
38 |
Correct |
6 ms |
3388 KB |
Output is correct |
39 |
Correct |
5 ms |
3284 KB |
Output is correct |
40 |
Correct |
6 ms |
3288 KB |
Output is correct |
41 |
Correct |
6 ms |
3360 KB |
Output is correct |
42 |
Correct |
5 ms |
3412 KB |
Output is correct |
43 |
Correct |
5 ms |
3356 KB |
Output is correct |
44 |
Correct |
6 ms |
3456 KB |
Output is correct |
45 |
Correct |
9 ms |
3844 KB |
Output is correct |
46 |
Correct |
9 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
2 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2484 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
2 ms |
2516 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Correct |
2 ms |
2492 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
5 ms |
2900 KB |
Output is correct |
34 |
Correct |
3 ms |
2900 KB |
Output is correct |
35 |
Correct |
6 ms |
3184 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
5 ms |
3136 KB |
Output is correct |
38 |
Correct |
6 ms |
3284 KB |
Output is correct |
39 |
Correct |
6 ms |
3284 KB |
Output is correct |
40 |
Correct |
5 ms |
3284 KB |
Output is correct |
41 |
Correct |
6 ms |
3336 KB |
Output is correct |
42 |
Correct |
5 ms |
3412 KB |
Output is correct |
43 |
Correct |
5 ms |
3392 KB |
Output is correct |
44 |
Correct |
7 ms |
3476 KB |
Output is correct |
45 |
Correct |
9 ms |
3796 KB |
Output is correct |
46 |
Correct |
9 ms |
3908 KB |
Output is correct |
47 |
Correct |
11 ms |
3956 KB |
Output is correct |
48 |
Correct |
5 ms |
3444 KB |
Output is correct |
49 |
Correct |
7 ms |
3412 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
12 ms |
4384 KB |
Output is correct |
52 |
Correct |
12 ms |
4304 KB |
Output is correct |
53 |
Correct |
7 ms |
3912 KB |
Output is correct |
54 |
Correct |
3 ms |
2772 KB |
Output is correct |
55 |
Correct |
3 ms |
2772 KB |
Output is correct |
56 |
Correct |
8 ms |
4092 KB |
Output is correct |
57 |
Correct |
2 ms |
3028 KB |
Output is correct |
58 |
Correct |
7 ms |
3512 KB |
Output is correct |
59 |
Correct |
7 ms |
3664 KB |
Output is correct |
60 |
Correct |
8 ms |
3664 KB |
Output is correct |
61 |
Correct |
7 ms |
3728 KB |
Output is correct |
62 |
Correct |
12 ms |
4052 KB |
Output is correct |
63 |
Correct |
32 ms |
4360 KB |
Output is correct |
64 |
Correct |
35 ms |
4404 KB |
Output is correct |
65 |
Correct |
44 ms |
5580 KB |
Output is correct |
66 |
Correct |
81 ms |
5964 KB |
Output is correct |
67 |
Correct |
64 ms |
6004 KB |
Output is correct |