#include <bits/stdc++.h>
using namespace std;
int inf = 1e9;
int n,m;
int b[30005],p[30005];
vector<int> what[30005];
set<int> resturi[30005];
vector<int> r[30005];
vector<int> noduri[30005];
int splv[30005];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> b[i] >> p[i],what[b[i]].push_back(p[i]);
for (int i = 1; i <= m; i++)
resturi[p[i]].insert(b[i] % p[i]);
for (int i = 1; i < n; i++)
for (auto it : resturi[i])
r[i].push_back(it);
for (int i = 0; i < n; i++)
noduri[0].push_back(i);
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n; j += i)
{
for (auto it : r[i])
if (it + j < n)
noduri[i].push_back(j + it);
}
}
int N = 0;
for (int i = 0; i < n; i++)
N += noduri[i].size();
vector<int> d(N);
for (int i = 0; i < N; i++)
d[i] = inf;
d[b[1]] = 0;
vector<bool> luat(N);
vector<pair<int,int>> cine;///inaltimea si pozitia in cadrul noduri[h]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < noduri[i].size(); j++)
cine.push_back({i,j});
}
deque<int> dq;
dq.push_back(b[1]);
while (!dq.empty())
{
int nod = dq.front();
dq.pop_front();
if (luat[nod])
continue;
luat[nod] = true;
if (nod >= n)
{
///nu e primul nivel
pair<int,int> pos = cine[nod];
int i = noduri[pos.first][pos.second];
if (d[i] > d[nod])
{
d[i] = d[nod];
dq.push_front(i);
}
if (i - pos.first >= 0)
{
int vec = nod - (int)r[pos.first].size();
if (d[vec] > d[nod] + 1)
{
d[vec] = d[nod] + 1;
dq.push_back(vec);
}
}
if (i + pos.first < n)
{
int vec = nod + (int)r[pos.first].size();
if (d[vec] > d[nod] + 1)
{
d[vec] = d[nod] + 1;
dq.push_back(vec);
}
}
}
else
{
///primul nivel
for (auto it : what[nod])
{
if (it >= n)
continue;
int vec = 0,pas = 1 << 25;
while (pas != 0)
{
if (vec + pas < N)
{
pair<int,int> xx = {cine[vec + pas].first,noduri[cine[vec + pas].first][cine[vec + pas].second]};
pair<int,int> yy = {it,nod};
if (xx <= yy)
vec += pas;
}
pas /= 2;
}
if (d[vec] > d[nod])
{
d[vec] = d[nod];
dq.push_front(vec);
}
}
}
}
if (d[b[2]] == inf)
cout << -1;
else
cout << d[b[2]];
return 0;
}
/**
Mama lor de limite...
Pai bun, complexitatea e O(Nsqrt), acum ca sa fie si constanta mai buna pot face gen:
- pun pe fiecare inaltime ce cladiri am nevoie
- ordinea nodurilor din graf va fi sortata dupa h, la eg dupa i
- asta imi face viata simpla pentru ca nodul din st / dr lui x = (h,i) este x +- (cate resturi modulo h apar in alea de la h)
**/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int j = 0; j < noduri[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3988 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3956 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3988 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
2 ms |
3928 KB |
Output is correct |
13 |
Correct |
1 ms |
3928 KB |
Output is correct |
14 |
Correct |
2 ms |
4184 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3988 KB |
Output is correct |
3 |
Correct |
1 ms |
3940 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
4184 KB |
Output is correct |
15 |
Correct |
2 ms |
4248 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4188 KB |
Output is correct |
19 |
Correct |
1 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4500 KB |
Output is correct |
26 |
Correct |
1 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
3 ms |
4700 KB |
Output is correct |
29 |
Correct |
4 ms |
5080 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4824 KB |
Output is correct |
32 |
Correct |
2 ms |
4568 KB |
Output is correct |
33 |
Correct |
5 ms |
6208 KB |
Output is correct |
34 |
Correct |
4 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3988 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3960 KB |
Output is correct |
6 |
Correct |
1 ms |
3772 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3984 KB |
Output is correct |
9 |
Correct |
1 ms |
3928 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4188 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4304 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4252 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
3 ms |
4700 KB |
Output is correct |
29 |
Correct |
3 ms |
5080 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4824 KB |
Output is correct |
32 |
Correct |
2 ms |
4564 KB |
Output is correct |
33 |
Correct |
4 ms |
6112 KB |
Output is correct |
34 |
Correct |
4 ms |
6112 KB |
Output is correct |
35 |
Correct |
16 ms |
7892 KB |
Output is correct |
36 |
Correct |
3 ms |
4700 KB |
Output is correct |
37 |
Correct |
15 ms |
9240 KB |
Output is correct |
38 |
Correct |
23 ms |
9932 KB |
Output is correct |
39 |
Correct |
22 ms |
9936 KB |
Output is correct |
40 |
Correct |
23 ms |
9932 KB |
Output is correct |
41 |
Correct |
25 ms |
10192 KB |
Output is correct |
42 |
Correct |
7 ms |
4696 KB |
Output is correct |
43 |
Correct |
5 ms |
4696 KB |
Output is correct |
44 |
Correct |
5 ms |
4660 KB |
Output is correct |
45 |
Correct |
27 ms |
14288 KB |
Output is correct |
46 |
Correct |
31 ms |
15312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3996 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4188 KB |
Output is correct |
19 |
Correct |
1 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Correct |
1 ms |
4188 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4316 KB |
Output is correct |
26 |
Correct |
2 ms |
4260 KB |
Output is correct |
27 |
Correct |
2 ms |
4304 KB |
Output is correct |
28 |
Correct |
3 ms |
4696 KB |
Output is correct |
29 |
Correct |
3 ms |
5080 KB |
Output is correct |
30 |
Correct |
2 ms |
4380 KB |
Output is correct |
31 |
Correct |
2 ms |
4824 KB |
Output is correct |
32 |
Correct |
2 ms |
4568 KB |
Output is correct |
33 |
Correct |
5 ms |
6160 KB |
Output is correct |
34 |
Correct |
4 ms |
6112 KB |
Output is correct |
35 |
Correct |
13 ms |
7892 KB |
Output is correct |
36 |
Correct |
3 ms |
4768 KB |
Output is correct |
37 |
Correct |
16 ms |
9168 KB |
Output is correct |
38 |
Correct |
21 ms |
9936 KB |
Output is correct |
39 |
Correct |
19 ms |
9912 KB |
Output is correct |
40 |
Correct |
21 ms |
9900 KB |
Output is correct |
41 |
Correct |
21 ms |
9936 KB |
Output is correct |
42 |
Correct |
5 ms |
4700 KB |
Output is correct |
43 |
Correct |
5 ms |
4768 KB |
Output is correct |
44 |
Correct |
5 ms |
4696 KB |
Output is correct |
45 |
Correct |
36 ms |
14288 KB |
Output is correct |
46 |
Correct |
28 ms |
14292 KB |
Output is correct |
47 |
Correct |
49 ms |
21348 KB |
Output is correct |
48 |
Correct |
13 ms |
10960 KB |
Output is correct |
49 |
Correct |
11 ms |
10192 KB |
Output is correct |
50 |
Correct |
9 ms |
9448 KB |
Output is correct |
51 |
Correct |
34 ms |
15428 KB |
Output is correct |
52 |
Correct |
33 ms |
15816 KB |
Output is correct |
53 |
Correct |
19 ms |
8916 KB |
Output is correct |
54 |
Correct |
4 ms |
5148 KB |
Output is correct |
55 |
Correct |
4 ms |
5844 KB |
Output is correct |
56 |
Correct |
9 ms |
6612 KB |
Output is correct |
57 |
Correct |
19 ms |
12816 KB |
Output is correct |
58 |
Correct |
7 ms |
6328 KB |
Output is correct |
59 |
Correct |
9 ms |
6608 KB |
Output is correct |
60 |
Correct |
10 ms |
7896 KB |
Output is correct |
61 |
Correct |
14 ms |
8152 KB |
Output is correct |
62 |
Correct |
30 ms |
16076 KB |
Output is correct |
63 |
Correct |
131 ms |
59096 KB |
Output is correct |
64 |
Correct |
132 ms |
62648 KB |
Output is correct |
65 |
Correct |
195 ms |
72112 KB |
Output is correct |
66 |
Correct |
288 ms |
130228 KB |
Output is correct |
67 |
Correct |
307 ms |
130280 KB |
Output is correct |