#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int radical=401,N=3e4+1;
vector<int>adj[N];
int dp[N][radical+1],n,m,best[N];
struct merg
{
int nod,val,cost;
};
void asoc(int &a,int &b,int &c,merg &x)
{
a=x.nod;
b=x.val;
c=x.cost;
}
void init(int n)
{
for(int i=1; i<=n; i++)
best[i]=1e9;
for(int i=1; i<=n; i++)
for(int j=1; j<=radical; j++)
dp[i][j]=1e9;
}
struct doge
{
int poz,val;
} dog[N];
void citeste()
{
cin>>n>>m;
init(n);
for(int i=1; i<=m; i++)
{
int a,b;
cin>>dog[i].poz>>dog[i].val;
dog[i].poz++;
adj[dog[i].poz].push_back(dog[i].val);
}
}
void rezolva()
{
queue<merg>q;
long long vals=-1;
if(dog[1].val<=radical)
vals=dog[1].val;
else
{
int ctc=1;
int nod=dog[1].poz;
int u=dog[1].val;
for(int j=nod+u; j<=n; j+=u)
{
if(best[j]>ctc)
{
best[j]=ctc;
q.push({j,-1,ctc});
}
ctc++;
}
ctc=1;
for(int j=nod-u; j>=1; j-=u)
{
if(best[j]>ctc)
{
best[j]=ctc;
q.push({j,-1,ctc});
}
ctc++;
}
}
q.push({dog[1].poz,vals,0});
while(!q.empty())
{
int nod,valoare,cost;
asoc(nod,valoare,cost,q.front());
best[nod]=min(best[nod],cost);
if(valoare!=-1)
{
if(nod+valoare<=n && dp[nod+valoare][valoare]>cost+1)
{
dp[nod+valoare][valoare]=cost+1;
best[nod+valoare]=min(best[nod+valoare],cost+1);
q.push({nod+valoare,valoare,cost+1});
}
if(nod-valoare>=1 && dp[nod-valoare][valoare]>cost+1)
{
dp[nod-valoare][valoare]=cost+1;
best[nod-valoare]=min(best[nod-valoare],cost+1);
q.push({nod-valoare,valoare,cost+1});
}
}
for(auto u:adj[nod])
{
if(u<=radical)
dp[nod][u]=min(dp[nod][u],cost);
if(u>radical)
{
int ctc=1;
for(int j=nod+u; j<=n; j+=u)
{
if(best[j]>cost+ctc)
{
best[j]=cost+ctc;
q.push({j,-1,cost+ctc});
}
ctc++;
}
ctc=1;
for(int j=nod-u; j>=1; j-=u)
{
if(best[j]>cost+ctc)
{
best[j]=cost+ctc;
q.push({j,-1,cost+ctc});
}
ctc++;
}
}
else
{
if(nod+u<=n &&dp[nod+u][u]>cost+1)
{
dp[nod+u][u]=cost+1;
q.push({nod+u,u,cost+1});
}
if(nod-u>=1&&dp[nod-u][u]>cost+1)
{
dp[nod-u][u]=cost+1;
q.push({nod-u,u,cost+1});
}
}
}
q.pop();
}
if(best[dog[2].poz]==1e9)
cout<<-1<<'\n';
else
cout<<best[dog[2].poz]<<'\n';
}
int main()
{
citeste();
rezolva();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void citeste()':
skyscraper.cpp:37:13: warning: unused variable 'a' [-Wunused-variable]
37 | int a,b;
| ^
skyscraper.cpp:37:15: warning: unused variable 'b' [-Wunused-variable]
37 | int a,b;
| ^
skyscraper.cpp: In function 'void rezolva()':
skyscraper.cpp:76:24: warning: narrowing conversion of 'vals' from 'long long int' to 'int' [-Wnarrowing]
76 | q.push({dog[1].poz,vals,0});
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3152 KB |
Output is correct |
2 |
Correct |
1 ms |
3152 KB |
Output is correct |
3 |
Correct |
2 ms |
3320 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
1 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3152 KB |
Output is correct |
2 |
Correct |
2 ms |
3152 KB |
Output is correct |
3 |
Correct |
1 ms |
3152 KB |
Output is correct |
4 |
Correct |
2 ms |
3152 KB |
Output is correct |
5 |
Correct |
1 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
2996 KB |
Output is correct |
7 |
Correct |
1 ms |
3152 KB |
Output is correct |
8 |
Correct |
1 ms |
3152 KB |
Output is correct |
9 |
Correct |
1 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
2 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3320 KB |
Output is correct |
13 |
Correct |
3 ms |
3320 KB |
Output is correct |
14 |
Correct |
2 ms |
3320 KB |
Output is correct |
15 |
Correct |
2 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3152 KB |
Output is correct |
2 |
Correct |
1 ms |
3152 KB |
Output is correct |
3 |
Correct |
1 ms |
3152 KB |
Output is correct |
4 |
Correct |
2 ms |
3152 KB |
Output is correct |
5 |
Correct |
1 ms |
3152 KB |
Output is correct |
6 |
Correct |
2 ms |
3196 KB |
Output is correct |
7 |
Correct |
1 ms |
3152 KB |
Output is correct |
8 |
Correct |
2 ms |
3152 KB |
Output is correct |
9 |
Correct |
1 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
2 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3152 KB |
Output is correct |
14 |
Correct |
3 ms |
3152 KB |
Output is correct |
15 |
Correct |
2 ms |
3152 KB |
Output is correct |
16 |
Correct |
2 ms |
3152 KB |
Output is correct |
17 |
Correct |
3 ms |
3152 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5368 KB |
Output is correct |
21 |
Correct |
3 ms |
3320 KB |
Output is correct |
22 |
Correct |
2 ms |
5248 KB |
Output is correct |
23 |
Correct |
2 ms |
5200 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
3 ms |
5200 KB |
Output is correct |
26 |
Correct |
3 ms |
5200 KB |
Output is correct |
27 |
Correct |
3 ms |
5200 KB |
Output is correct |
28 |
Correct |
3 ms |
5200 KB |
Output is correct |
29 |
Correct |
3 ms |
5200 KB |
Output is correct |
30 |
Correct |
3 ms |
5368 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
2 ms |
5200 KB |
Output is correct |
33 |
Correct |
4 ms |
5200 KB |
Output is correct |
34 |
Correct |
4 ms |
5176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3320 KB |
Output is correct |
2 |
Correct |
1 ms |
3324 KB |
Output is correct |
3 |
Correct |
1 ms |
3152 KB |
Output is correct |
4 |
Correct |
2 ms |
3152 KB |
Output is correct |
5 |
Correct |
2 ms |
3152 KB |
Output is correct |
6 |
Correct |
2 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3152 KB |
Output is correct |
8 |
Correct |
2 ms |
3324 KB |
Output is correct |
9 |
Correct |
2 ms |
3152 KB |
Output is correct |
10 |
Correct |
1 ms |
3152 KB |
Output is correct |
11 |
Correct |
2 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3152 KB |
Output is correct |
14 |
Correct |
2 ms |
3320 KB |
Output is correct |
15 |
Correct |
2 ms |
3152 KB |
Output is correct |
16 |
Correct |
2 ms |
3152 KB |
Output is correct |
17 |
Correct |
3 ms |
3320 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
5200 KB |
Output is correct |
21 |
Correct |
2 ms |
3152 KB |
Output is correct |
22 |
Correct |
2 ms |
5200 KB |
Output is correct |
23 |
Correct |
2 ms |
5200 KB |
Output is correct |
24 |
Correct |
4 ms |
5200 KB |
Output is correct |
25 |
Correct |
3 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5200 KB |
Output is correct |
27 |
Correct |
3 ms |
5372 KB |
Output is correct |
28 |
Correct |
5 ms |
5200 KB |
Output is correct |
29 |
Correct |
3 ms |
5200 KB |
Output is correct |
30 |
Correct |
3 ms |
5200 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5372 KB |
Output is correct |
33 |
Correct |
4 ms |
5200 KB |
Output is correct |
34 |
Correct |
6 ms |
5200 KB |
Output is correct |
35 |
Correct |
22 ms |
5712 KB |
Output is correct |
36 |
Correct |
6 ms |
3152 KB |
Output is correct |
37 |
Correct |
43 ms |
5840 KB |
Output is correct |
38 |
Correct |
44 ms |
5712 KB |
Output is correct |
39 |
Correct |
42 ms |
5712 KB |
Output is correct |
40 |
Correct |
52 ms |
5724 KB |
Output is correct |
41 |
Correct |
34 ms |
5712 KB |
Output is correct |
42 |
Correct |
10 ms |
5456 KB |
Output is correct |
43 |
Correct |
9 ms |
5456 KB |
Output is correct |
44 |
Correct |
16 ms |
5560 KB |
Output is correct |
45 |
Correct |
24 ms |
5968 KB |
Output is correct |
46 |
Correct |
29 ms |
5968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3152 KB |
Output is correct |
2 |
Correct |
1 ms |
3152 KB |
Output is correct |
3 |
Correct |
1 ms |
3152 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
1 ms |
2948 KB |
Output is correct |
6 |
Correct |
1 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3152 KB |
Output is correct |
8 |
Correct |
2 ms |
3152 KB |
Output is correct |
9 |
Correct |
1 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
3 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3152 KB |
Output is correct |
14 |
Correct |
2 ms |
3152 KB |
Output is correct |
15 |
Correct |
3 ms |
3152 KB |
Output is correct |
16 |
Correct |
2 ms |
3320 KB |
Output is correct |
17 |
Correct |
2 ms |
3324 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
5200 KB |
Output is correct |
21 |
Correct |
2 ms |
3152 KB |
Output is correct |
22 |
Correct |
2 ms |
5200 KB |
Output is correct |
23 |
Correct |
2 ms |
5200 KB |
Output is correct |
24 |
Correct |
3 ms |
5200 KB |
Output is correct |
25 |
Correct |
2 ms |
5200 KB |
Output is correct |
26 |
Correct |
3 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
3 ms |
5200 KB |
Output is correct |
29 |
Correct |
3 ms |
5200 KB |
Output is correct |
30 |
Correct |
3 ms |
5200 KB |
Output is correct |
31 |
Correct |
4 ms |
5200 KB |
Output is correct |
32 |
Correct |
2 ms |
5200 KB |
Output is correct |
33 |
Correct |
7 ms |
5200 KB |
Output is correct |
34 |
Correct |
4 ms |
5200 KB |
Output is correct |
35 |
Correct |
22 ms |
5552 KB |
Output is correct |
36 |
Correct |
4 ms |
3152 KB |
Output is correct |
37 |
Correct |
43 ms |
5924 KB |
Output is correct |
38 |
Correct |
52 ms |
5712 KB |
Output is correct |
39 |
Correct |
49 ms |
5712 KB |
Output is correct |
40 |
Correct |
39 ms |
5880 KB |
Output is correct |
41 |
Correct |
40 ms |
5712 KB |
Output is correct |
42 |
Correct |
10 ms |
5456 KB |
Output is correct |
43 |
Correct |
9 ms |
5468 KB |
Output is correct |
44 |
Correct |
13 ms |
5504 KB |
Output is correct |
45 |
Correct |
21 ms |
5968 KB |
Output is correct |
46 |
Correct |
23 ms |
5992 KB |
Output is correct |
47 |
Correct |
205 ms |
22352 KB |
Output is correct |
48 |
Correct |
29 ms |
38480 KB |
Output is correct |
49 |
Correct |
17 ms |
42796 KB |
Output is correct |
50 |
Correct |
15 ms |
46544 KB |
Output is correct |
51 |
Correct |
63 ms |
49684 KB |
Output is correct |
52 |
Correct |
113 ms |
49488 KB |
Output is correct |
53 |
Correct |
26 ms |
49104 KB |
Output is correct |
54 |
Correct |
16 ms |
48480 KB |
Output is correct |
55 |
Correct |
11 ms |
48464 KB |
Output is correct |
56 |
Correct |
23 ms |
49440 KB |
Output is correct |
57 |
Correct |
22 ms |
48464 KB |
Output is correct |
58 |
Correct |
18 ms |
48720 KB |
Output is correct |
59 |
Correct |
22 ms |
48720 KB |
Output is correct |
60 |
Correct |
19 ms |
48720 KB |
Output is correct |
61 |
Correct |
21 ms |
48736 KB |
Output is correct |
62 |
Correct |
36 ms |
49488 KB |
Output is correct |
63 |
Correct |
84 ms |
49488 KB |
Output is correct |
64 |
Correct |
111 ms |
49464 KB |
Output is correct |
65 |
Correct |
129 ms |
49224 KB |
Output is correct |
66 |
Correct |
173 ms |
49268 KB |
Output is correct |
67 |
Correct |
197 ms |
49224 KB |
Output is correct |