// created by Dinh Manh Hung
// onepunchac168
// THPT CHUYEN HA TINH
// HA TINH, VIET NAM
// Master go go
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
#pragma GCC target("sse4,avx2,fma")
#define task ""
#define ldb long double
#define pb push_back
#define fi first
#define se second
#define pc pop_back()
#define all(x) begin(x),end(x)
#define uniquev(v) v.resize(unique(all(v))-v.begin())
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define cntbit(v) __builtin_popcountll(v)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((1LL*a*b)/__gcd(a,b))
#define mask(x) (1LL<<(x))
#define readbit(x,i) ((x>>i)&1)
#define ins insert
typedef int ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
typedef pair <ii,ii> iiii;
ll dx[]= {1,-1,0,0,1,-1,1,-1};
ll dy[]= {0,0,-1,1,1,-1,-1,1};
const ldb PI = acos (-1);
//const ll mod=978846151;
//const ll base=29;
const int maxn=1e6+5;
const int mod=1e9+7;
const char nl = '\n';
inline int ReadInt()
{
char co;
for (co = getchar(); co < '0' || co > '9'; co = getchar());
int xo = co - '0';
for (co = getchar(); co >= '0' && co <= '9'; co = getchar())
xo = xo * 10 + co - '0';
return xo;
}
void WriteInt(int xo)
{
if (xo > 9)
WriteInt(xo / 10);
putchar(xo % 10 + '0');
}
/* END OF TEMPLATE*/
// ================= Solution =================//
ll n,m;
ll b[50005];
ll p[50005];
ll dp[50005][255];
bool fee[50005][255];
ll BLOCK=50;
vector <ll> vt[50005];
ll id=0;
priority_queue< iii,vector <iii> ,greater <iii> > qu;
void optmushnpr()
{
cin>>n>>m;
for (int i=0;i<m;i++)
{
cin>>b[i]>>p[i];
if (p[i]<=BLOCK){
fee[b[i]][p[i]]=1;
}
else vt[b[i]].pb(p[i]);
}
id=b[1];
for (int i=0;i<n;i++)
{
for (int j=1;j<=BLOCK+1;j++)
{
dp[i][j]=1e9+5;
}
}
ll aa=b[0];
ll bb=(p[0]>BLOCK)? BLOCK+1 : p[0];
qu.push({0,{aa,bb}});
dp[aa][bb]=0;
ll rr=1e9+5;
while (!qu.empty())
{
ll u=qu.top().se.fi;
ll v=qu.top().se.se;
qu.pop();
if (u==id)
{
rr=dp[u][v];
break;
}
if (v==BLOCK+1)
{
for (auto cnt:vt[u])
{
ll id=u,dem=0;
while (id+cnt<n)
{
id+=cnt;
dem++;
if (dp[id][v]>dp[u][v]+dem)
{
dp[id][v]=dp[u][v]+dem;
qu.push({dp[id][v],{id,v}});
}
}
id=u,dem=0;
while (id-cnt>=0)
{
id-=cnt;
dem++;
if (dp[id][v]>dp[u][v]+dem)
{
dp[id][v]=dp[u][v]+dem;
qu.push({dp[id][v],{id,v}});
}
}
}
for (int i=1;i<=BLOCK;i++)
{
if (fee[u][i]==1)
{
if (u+i<n){
if (dp[u+i][i]>dp[u][v]+1)
{
dp[u+i][i]=dp[u][v]+1;
qu.push({dp[u+i][i],{u+i,i}});
}
}
if (u-i>=0)
{
if (dp[u-i][i]>dp[u][v]+1){
dp[u-i][i]=dp[u][v]+1;
qu.push({dp[u-i][i],{u-i,i}});
}
}
}
}
}
else
{
if (dp[u][BLOCK+1]>dp[u][v])
{
dp[u][BLOCK+1]=dp[u][v];
qu.push({dp[u][BLOCK+1],{u,BLOCK+1}});
}
if (u+v<n)
{
if (dp[u+v][v]>dp[u][v]+1)
{
dp[u+v][v]=dp[u][v]+1;
qu.push({dp[u+v][v],{u+v,v}});
}
}
if (u-v>=0)
{
if (dp[u-v][v]>dp[u][v]+1)
{
dp[u-v][v]=dp[u][v]+1;
qu.push({dp[u-v][v],{u-v,v}});
}
}
}
}
if (rr==1e9+5)
{
cout<<-1;
}
else cout<<rr;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);}
int t;
t=1;
//cin>>t;
while (t--){optmushnpr();}
}
// goodbye see ya
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:192:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:193:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen(task".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1504 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
2 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1536 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
2 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
2 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
4 ms |
3924 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
3 ms |
3540 KB |
Output is correct |
25 |
Correct |
2 ms |
3540 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
8 ms |
3580 KB |
Output is correct |
30 |
Correct |
4 ms |
3540 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3540 KB |
Output is correct |
33 |
Correct |
12 ms |
3692 KB |
Output is correct |
34 |
Correct |
9 ms |
3652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
2 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3924 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
3260 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
4 ms |
3540 KB |
Output is correct |
25 |
Correct |
2 ms |
3540 KB |
Output is correct |
26 |
Correct |
3 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
8 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3540 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3552 KB |
Output is correct |
33 |
Correct |
12 ms |
3668 KB |
Output is correct |
34 |
Correct |
9 ms |
3668 KB |
Output is correct |
35 |
Correct |
7 ms |
3796 KB |
Output is correct |
36 |
Correct |
2 ms |
2900 KB |
Output is correct |
37 |
Correct |
6 ms |
4344 KB |
Output is correct |
38 |
Correct |
8 ms |
4564 KB |
Output is correct |
39 |
Correct |
7 ms |
4436 KB |
Output is correct |
40 |
Correct |
7 ms |
4436 KB |
Output is correct |
41 |
Correct |
8 ms |
4564 KB |
Output is correct |
42 |
Correct |
5 ms |
3796 KB |
Output is correct |
43 |
Correct |
5 ms |
3796 KB |
Output is correct |
44 |
Correct |
5 ms |
4180 KB |
Output is correct |
45 |
Correct |
16 ms |
4100 KB |
Output is correct |
46 |
Correct |
8 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1528 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1632 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
2 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3924 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
3336 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
3 ms |
3540 KB |
Output is correct |
25 |
Correct |
2 ms |
3540 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
8 ms |
3580 KB |
Output is correct |
30 |
Correct |
3 ms |
3540 KB |
Output is correct |
31 |
Correct |
5 ms |
3572 KB |
Output is correct |
32 |
Correct |
4 ms |
3540 KB |
Output is correct |
33 |
Correct |
12 ms |
3668 KB |
Output is correct |
34 |
Correct |
8 ms |
3668 KB |
Output is correct |
35 |
Correct |
6 ms |
3796 KB |
Output is correct |
36 |
Correct |
3 ms |
2900 KB |
Output is correct |
37 |
Correct |
6 ms |
4360 KB |
Output is correct |
38 |
Correct |
7 ms |
4580 KB |
Output is correct |
39 |
Correct |
9 ms |
4436 KB |
Output is correct |
40 |
Correct |
7 ms |
4440 KB |
Output is correct |
41 |
Correct |
7 ms |
4564 KB |
Output is correct |
42 |
Correct |
5 ms |
3796 KB |
Output is correct |
43 |
Correct |
5 ms |
3796 KB |
Output is correct |
44 |
Correct |
5 ms |
4180 KB |
Output is correct |
45 |
Correct |
17 ms |
4052 KB |
Output is correct |
46 |
Correct |
9 ms |
4180 KB |
Output is correct |
47 |
Correct |
15 ms |
16288 KB |
Output is correct |
48 |
Correct |
15 ms |
25428 KB |
Output is correct |
49 |
Correct |
16 ms |
27512 KB |
Output is correct |
50 |
Correct |
15 ms |
29908 KB |
Output is correct |
51 |
Correct |
30 ms |
33016 KB |
Output is correct |
52 |
Correct |
30 ms |
32972 KB |
Output is correct |
53 |
Correct |
20 ms |
32212 KB |
Output is correct |
54 |
Correct |
15 ms |
31444 KB |
Output is correct |
55 |
Correct |
15 ms |
31396 KB |
Output is correct |
56 |
Correct |
21 ms |
39124 KB |
Output is correct |
57 |
Correct |
16 ms |
36692 KB |
Output is correct |
58 |
Correct |
21 ms |
31620 KB |
Output is correct |
59 |
Correct |
21 ms |
31700 KB |
Output is correct |
60 |
Correct |
22 ms |
31740 KB |
Output is correct |
61 |
Correct |
20 ms |
31720 KB |
Output is correct |
62 |
Correct |
35 ms |
32632 KB |
Output is correct |
63 |
Correct |
65 ms |
33608 KB |
Output is correct |
64 |
Correct |
66 ms |
33580 KB |
Output is correct |
65 |
Correct |
92 ms |
35092 KB |
Output is correct |
66 |
Correct |
257 ms |
35076 KB |
Output is correct |
67 |
Correct |
123 ms |
35140 KB |
Output is correct |