#include <bits/stdc++.h>
using namespace std;
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<int>
#define dos pair<int,int>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
int n,v;
set<int>scrapes[30106];
vector<dos>scrapes1[30106];
int dist[30106];
void preprocess(int tower,int range)
{
int weight=1;
for (int i = tower+range; i < n; i+=range)
{
scrapes1[tower].pb({i,weight});
weight++;
if(scrapes[i].find(range)!=scrapes[i].end())break;
}
weight=1;
for (int i = tower-range; i >= 0; i-=range)
{
scrapes1[tower].pb({i,weight});
weight++;
if(scrapes[i].find(range)!=scrapes[i].end())break;
}
}
int start[30101],endi[30101];
int main()
{
flash;
cin>>n>>v;
int rooto,dest;
for (int i = 0; i < v; ++i)
{
int x,y;
cin>>x>>y;
start[i]=x,endi[i]=y;
if(i==0)rooto=x;
else if(i==1)dest=x;
}
for (int i = 0; i < v; ++i)
{
if(start[i]!=dest)
scrapes[start[i]].insert(endi[i]);
}
for (int i = 0; i < n; ++i)
{
if(i!=dest)
{for(auto it:scrapes[i])
preprocess(i,it);}
}
for (int i = 0; i < n; ++i)
{
dist[i]=INF;
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>g;
g.push({0,rooto});
dist[rooto]=0;
while(!g.empty())
{
auto u = g.top();
auto tower = u.second;
g.pop();
if(dist[tower]<u.first)continue;
if(tower==dest)break;
for (auto k:scrapes1[tower])
{
if(dist[k.first]>u.first+k.second)
{
dist[k.first]=u.first+k.second;
g.push({u.first+k.second,k.first});
}
}
}
int ans=INF;
//debugs(rooto,dest);
ans=min(ans,dist[dest]);
if(ans==INF)cout<<-1;
else cout<<ans;
return 0;
}
//code the AC sol !
// BS/queue/map
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:81:5: warning: 'dest' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(tower==dest)break;
^~
skyscraper.cpp:74:14: warning: 'rooto' may be used uninitialized in this function [-Wmaybe-uninitialized]
dist[rooto]=0;
~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2428 KB |
Output is correct |
2 |
Correct |
4 ms |
2296 KB |
Output is correct |
3 |
Correct |
5 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
4 ms |
2424 KB |
Output is correct |
6 |
Correct |
5 ms |
2296 KB |
Output is correct |
7 |
Correct |
6 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
0 ms |
2428 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
4 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2424 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2552 KB |
Output is correct |
10 |
Correct |
4 ms |
2552 KB |
Output is correct |
11 |
Correct |
5 ms |
2552 KB |
Output is correct |
12 |
Correct |
4 ms |
2552 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
6 ms |
2552 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
3 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2424 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2428 KB |
Output is correct |
11 |
Correct |
5 ms |
2552 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
1 ms |
2680 KB |
Output is correct |
16 |
Correct |
8 ms |
2596 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
7 ms |
2680 KB |
Output is correct |
20 |
Correct |
3 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
2552 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
5 ms |
2552 KB |
Output is correct |
27 |
Correct |
4 ms |
2552 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
9 ms |
3320 KB |
Output is correct |
30 |
Correct |
6 ms |
2808 KB |
Output is correct |
31 |
Correct |
7 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2812 KB |
Output is correct |
33 |
Correct |
10 ms |
4084 KB |
Output is correct |
34 |
Correct |
9 ms |
4084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2428 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
3 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
4 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2424 KB |
Output is correct |
8 |
Correct |
4 ms |
2552 KB |
Output is correct |
9 |
Correct |
2 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2552 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2552 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2552 KB |
Output is correct |
17 |
Correct |
3 ms |
2808 KB |
Output is correct |
18 |
Correct |
3 ms |
2680 KB |
Output is correct |
19 |
Correct |
1 ms |
2552 KB |
Output is correct |
20 |
Correct |
4 ms |
2652 KB |
Output is correct |
21 |
Correct |
4 ms |
2552 KB |
Output is correct |
22 |
Correct |
12 ms |
2680 KB |
Output is correct |
23 |
Correct |
12 ms |
2648 KB |
Output is correct |
24 |
Correct |
6 ms |
2552 KB |
Output is correct |
25 |
Correct |
4 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2556 KB |
Output is correct |
27 |
Correct |
5 ms |
2552 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
8 ms |
3320 KB |
Output is correct |
30 |
Correct |
1 ms |
2808 KB |
Output is correct |
31 |
Correct |
6 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
11 ms |
4088 KB |
Output is correct |
34 |
Correct |
9 ms |
4084 KB |
Output is correct |
35 |
Correct |
24 ms |
4984 KB |
Output is correct |
36 |
Correct |
6 ms |
2936 KB |
Output is correct |
37 |
Correct |
27 ms |
5624 KB |
Output is correct |
38 |
Correct |
32 ms |
5980 KB |
Output is correct |
39 |
Correct |
32 ms |
5880 KB |
Output is correct |
40 |
Correct |
32 ms |
5880 KB |
Output is correct |
41 |
Correct |
32 ms |
5880 KB |
Output is correct |
42 |
Correct |
7 ms |
2808 KB |
Output is correct |
43 |
Correct |
10 ms |
3064 KB |
Output is correct |
44 |
Correct |
10 ms |
3064 KB |
Output is correct |
45 |
Correct |
35 ms |
8824 KB |
Output is correct |
46 |
Correct |
33 ms |
8824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
4 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2504 KB |
Output is correct |
7 |
Correct |
4 ms |
2424 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2424 KB |
Output is correct |
11 |
Correct |
5 ms |
2552 KB |
Output is correct |
12 |
Correct |
5 ms |
2552 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
5 ms |
2552 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2552 KB |
Output is correct |
17 |
Correct |
12 ms |
2684 KB |
Output is correct |
18 |
Correct |
4 ms |
2552 KB |
Output is correct |
19 |
Correct |
4 ms |
2552 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2552 KB |
Output is correct |
22 |
Correct |
4 ms |
2552 KB |
Output is correct |
23 |
Correct |
5 ms |
2552 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2556 KB |
Output is correct |
27 |
Correct |
5 ms |
2552 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
12 ms |
3320 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
6 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
11 ms |
4084 KB |
Output is correct |
34 |
Correct |
9 ms |
4084 KB |
Output is correct |
35 |
Correct |
24 ms |
4872 KB |
Output is correct |
36 |
Correct |
6 ms |
2936 KB |
Output is correct |
37 |
Correct |
28 ms |
5624 KB |
Output is correct |
38 |
Correct |
33 ms |
5924 KB |
Output is correct |
39 |
Correct |
32 ms |
5880 KB |
Output is correct |
40 |
Correct |
31 ms |
5880 KB |
Output is correct |
41 |
Correct |
32 ms |
5852 KB |
Output is correct |
42 |
Correct |
9 ms |
2808 KB |
Output is correct |
43 |
Correct |
12 ms |
2936 KB |
Output is correct |
44 |
Correct |
10 ms |
3064 KB |
Output is correct |
45 |
Correct |
34 ms |
8824 KB |
Output is correct |
46 |
Correct |
32 ms |
8824 KB |
Output is correct |
47 |
Correct |
58 ms |
13300 KB |
Output is correct |
48 |
Correct |
24 ms |
6376 KB |
Output is correct |
49 |
Correct |
21 ms |
5624 KB |
Output is correct |
50 |
Correct |
18 ms |
5112 KB |
Output is correct |
51 |
Correct |
43 ms |
8452 KB |
Output is correct |
52 |
Correct |
47 ms |
8764 KB |
Output is correct |
53 |
Correct |
18 ms |
5352 KB |
Output is correct |
54 |
Correct |
6 ms |
2936 KB |
Output is correct |
55 |
Correct |
7 ms |
3316 KB |
Output is correct |
56 |
Correct |
20 ms |
5372 KB |
Output is correct |
57 |
Correct |
28 ms |
11388 KB |
Output is correct |
58 |
Correct |
14 ms |
3696 KB |
Output is correct |
59 |
Correct |
15 ms |
4468 KB |
Output is correct |
60 |
Correct |
15 ms |
4852 KB |
Output is correct |
61 |
Correct |
14 ms |
4088 KB |
Output is correct |
62 |
Correct |
46 ms |
9484 KB |
Output is correct |
63 |
Correct |
106 ms |
26348 KB |
Output is correct |
64 |
Correct |
116 ms |
29872 KB |
Output is correct |
65 |
Correct |
149 ms |
39304 KB |
Output is correct |
66 |
Correct |
270 ms |
65896 KB |
Output is correct |
67 |
Correct |
217 ms |
65892 KB |
Output is correct |