#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=30100;
int n,m,b[N],p[N];
ll d[N];
VI qs[N];
int main() {
scanf("%d%d",&n,&m);
rep(i,0,m) {
scanf("%d%d",b+i,p+i);
qs[b[i]].pb(p[i]);
}
rep(i,0,n) d[i]=(1LL<<59);
priority_queue<pair<ll,int>> q;
q.push(mp(0,b[0])); d[b[0]]=0;
while (SZ(q)) {
auto x=q.top(); q.pop();
if (d[x.se]<-x.fi) continue;
for (int j:qs[x.se]) {
for (int u=x.se-j;u>=0;u-=j) {
if (d[u]>d[x.se]+(x.se-u)/j) {
d[u]=d[x.se]+(x.se-u)/j;
q.push(mp(-d[u],u));
}
}
for (int u=x.se+j;u<n;u+=j) {
if (d[u]>d[x.se]+(u-x.se)/j) {
d[u]=d[x.se]+(u-x.se)/j;
q.push(mp(-d[u],u));
}
}
}
}
if (d[b[1]]==(1LL<<59)) printf("-1");
else printf("%lld",d[b[1]]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d%d",b+i,p+i);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
25 ms |
1172 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
1000 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
24 ms |
1088 KB |
Output is correct |
27 |
Correct |
22 ms |
1092 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Correct |
3 ms |
1228 KB |
Output is correct |
34 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
916 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
25 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
24 ms |
1092 KB |
Output is correct |
27 |
Correct |
22 ms |
1084 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Correct |
2 ms |
1228 KB |
Output is correct |
34 |
Correct |
2 ms |
1356 KB |
Output is correct |
35 |
Correct |
6 ms |
1504 KB |
Output is correct |
36 |
Correct |
2 ms |
1048 KB |
Output is correct |
37 |
Correct |
6 ms |
1484 KB |
Output is correct |
38 |
Correct |
8 ms |
1600 KB |
Output is correct |
39 |
Correct |
8 ms |
1612 KB |
Output is correct |
40 |
Correct |
10 ms |
1628 KB |
Output is correct |
41 |
Correct |
9 ms |
1656 KB |
Output is correct |
42 |
Correct |
357 ms |
1484 KB |
Output is correct |
43 |
Correct |
354 ms |
1484 KB |
Output is correct |
44 |
Correct |
358 ms |
1404 KB |
Output is correct |
45 |
Correct |
8 ms |
1612 KB |
Output is correct |
46 |
Correct |
8 ms |
1700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
2 ms |
972 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1056 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
25 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
22 ms |
972 KB |
Output is correct |
27 |
Correct |
20 ms |
1084 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
1 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Correct |
3 ms |
1228 KB |
Output is correct |
34 |
Correct |
3 ms |
1356 KB |
Output is correct |
35 |
Correct |
7 ms |
1540 KB |
Output is correct |
36 |
Correct |
3 ms |
1100 KB |
Output is correct |
37 |
Correct |
7 ms |
1484 KB |
Output is correct |
38 |
Correct |
8 ms |
1612 KB |
Output is correct |
39 |
Correct |
8 ms |
1620 KB |
Output is correct |
40 |
Correct |
9 ms |
1716 KB |
Output is correct |
41 |
Correct |
8 ms |
1612 KB |
Output is correct |
42 |
Correct |
353 ms |
1484 KB |
Output is correct |
43 |
Correct |
351 ms |
1468 KB |
Output is correct |
44 |
Correct |
364 ms |
1484 KB |
Output is correct |
45 |
Correct |
8 ms |
1612 KB |
Output is correct |
46 |
Correct |
8 ms |
1612 KB |
Output is correct |
47 |
Correct |
20 ms |
2800 KB |
Output is correct |
48 |
Correct |
6 ms |
1740 KB |
Output is correct |
49 |
Correct |
6 ms |
1732 KB |
Output is correct |
50 |
Correct |
4 ms |
1612 KB |
Output is correct |
51 |
Correct |
25 ms |
4216 KB |
Output is correct |
52 |
Correct |
24 ms |
3144 KB |
Output is correct |
53 |
Correct |
14 ms |
3144 KB |
Output is correct |
54 |
Correct |
2 ms |
1612 KB |
Output is correct |
55 |
Correct |
3 ms |
1612 KB |
Output is correct |
56 |
Execution timed out |
1087 ms |
3016 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |