#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <complex>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define ld long long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 1000000007ll
#define INF 1e18
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast \
ios_base::sync_with_stdio (false); \
cin.tie (NULL); \
cout.tie (NULL)
using namespace std;
signed main()
{
fast;
ll n,m;
cin>>n>>m;
map <pair<ll,ll>,bool> mp;
ll loc[m+1], p[m+1];
set <ll> l[n];
set <ll> lt;
rep(i,1,m)
{
cin>>loc[i]>>p[i];
if(p[i]<n)
{
mp[{p[i],loc[i]%p[i]}]=true;
l[p[i]].insert(loc[i]);
}
lt.insert(loc[i]);
}
vector <pair<ll,ll>> adj[n];
for(auto [pr,b]:mp)
{
ll last=-1;
for(ll j=pr.ss;j<n;j+=pr.ff)
{
if(l[pr.ff].count(j))
{
if(last==-1)
last=j;
else
{
adj[last].pb({j,(j-last)/pr.ff});
adj[j].pb({last,(j-last)/pr.ff});
last=j;
}
}
}
last=-1;
ll f;
for(ll j=pr.ss;j<n;j+=pr.ff)
{
if(l[pr.ff].count(j))
{
last=j;
}
else if(lt.count(j) && last!=-1)
{
adj[last].pb({j,(j-last)/pr.ff});
}
f=j;
}
last=-1;
for(ll j=f;j>=0;j-=pr.ff)
{
if(l[pr.ff].count(j))
{
last=j;
}
else if(lt.count(j) && last!=-1)
{
adj[last].pb({j,(last-j)/pr.ff});
}
}
}
// rep(i,0,n-1)
// {
// cout<<i<<" : ";
// for(auto [a,b]:adj[i])
// cout<<a<<","<<b<<" ";
// cout<<endl;
// }
vector <ll> d(n,INF);
set <pair<ll,ll>> s;
s.insert({0,loc[1]});
d[loc[1]]=0;
while(!s.empty())
{
auto [dist,v]=(*s.begin());
s.erase({dist,v});
for(auto [u,e]:adj[v])
if(d[u]>e+d[v])
{
s.erase({d[u],u});
d[u]=e+d[v];
s.insert({d[u],u});
}
}
cout<<(d[loc[2]]==INF?-1:d[loc[2]])<<endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:98:23: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | for(ll j=f;j>=0;j-=pr.ff)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
3 ms |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
2 ms |
580 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
584 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
980 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
6 ms |
1228 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
444 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
5 ms |
772 KB |
Output is correct |
34 |
Correct |
6 ms |
708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
908 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
5 ms |
852 KB |
Output is correct |
25 |
Correct |
4 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
8 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
584 KB |
Output is correct |
30 |
Correct |
1 ms |
448 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
536 KB |
Output is correct |
33 |
Correct |
5 ms |
724 KB |
Output is correct |
34 |
Correct |
6 ms |
708 KB |
Output is correct |
35 |
Correct |
36 ms |
5092 KB |
Output is correct |
36 |
Correct |
5 ms |
1096 KB |
Output is correct |
37 |
Correct |
56 ms |
7056 KB |
Output is correct |
38 |
Correct |
58 ms |
7876 KB |
Output is correct |
39 |
Correct |
59 ms |
7884 KB |
Output is correct |
40 |
Correct |
61 ms |
7904 KB |
Output is correct |
41 |
Correct |
58 ms |
7928 KB |
Output is correct |
42 |
Correct |
5 ms |
1108 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
6 ms |
1364 KB |
Output is correct |
45 |
Correct |
42 ms |
5068 KB |
Output is correct |
46 |
Correct |
44 ms |
5084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
584 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
972 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
5 ms |
876 KB |
Output is correct |
25 |
Correct |
4 ms |
980 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
6 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
448 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
5 ms |
724 KB |
Output is correct |
34 |
Correct |
5 ms |
736 KB |
Output is correct |
35 |
Correct |
35 ms |
5124 KB |
Output is correct |
36 |
Correct |
5 ms |
1100 KB |
Output is correct |
37 |
Correct |
57 ms |
7088 KB |
Output is correct |
38 |
Correct |
58 ms |
7896 KB |
Output is correct |
39 |
Correct |
66 ms |
7980 KB |
Output is correct |
40 |
Correct |
58 ms |
7888 KB |
Output is correct |
41 |
Correct |
58 ms |
7824 KB |
Output is correct |
42 |
Correct |
5 ms |
1108 KB |
Output is correct |
43 |
Correct |
5 ms |
1096 KB |
Output is correct |
44 |
Correct |
6 ms |
1364 KB |
Output is correct |
45 |
Correct |
42 ms |
5064 KB |
Output is correct |
46 |
Correct |
43 ms |
5104 KB |
Output is correct |
47 |
Correct |
233 ms |
19788 KB |
Output is correct |
48 |
Correct |
71 ms |
8472 KB |
Output is correct |
49 |
Correct |
50 ms |
7072 KB |
Output is correct |
50 |
Correct |
42 ms |
6092 KB |
Output is correct |
51 |
Correct |
127 ms |
12484 KB |
Output is correct |
52 |
Correct |
145 ms |
13000 KB |
Output is correct |
53 |
Correct |
51 ms |
9556 KB |
Output is correct |
54 |
Correct |
2 ms |
2644 KB |
Output is correct |
55 |
Correct |
2 ms |
2624 KB |
Output is correct |
56 |
Correct |
26 ms |
7492 KB |
Output is correct |
57 |
Correct |
69 ms |
4256 KB |
Output is correct |
58 |
Correct |
6 ms |
3156 KB |
Output is correct |
59 |
Correct |
7 ms |
3284 KB |
Output is correct |
60 |
Correct |
11 ms |
3412 KB |
Output is correct |
61 |
Correct |
11 ms |
3796 KB |
Output is correct |
62 |
Correct |
174 ms |
17592 KB |
Output is correct |
63 |
Correct |
113 ms |
7116 KB |
Output is correct |
64 |
Correct |
124 ms |
6988 KB |
Output is correct |
65 |
Correct |
172 ms |
7240 KB |
Output is correct |
66 |
Correct |
265 ms |
7252 KB |
Output is correct |
67 |
Correct |
278 ms |
7224 KB |
Output is correct |