#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];
int 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 <= 2000; 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 <= 2000; 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)
{
if (d[nod - r[pos.first].size()] > d[nod] + 1)
{
d[nod - r[pos.first].size()] = d[nod] + 1;
dq.push_back(nod - r[pos.first].size());
}
}
if (i + pos.first < n)
{
if (d[nod + r[pos.first].size()] > d[nod] + 1)
{
d[nod + r[pos.first].size()] = d[nod] + 1;
dq.push_back(nod + r[pos.first].size());
}
}
}
else
{
///primul nivel
for (auto it : what[nod])
{
int vec = 0,pas = 1 << 30;
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 |
3928 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 |
2 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 |
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 |
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 |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
4212 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 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 |
3964 KB |
Output is correct |
4 |
Correct |
1 ms |
3748 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 |
4184 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
3 ms |
3980 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4272 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 |
4184 KB |
Output is correct |
19 |
Correct |
2 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 |
Incorrect |
2 ms |
4188 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 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 |
3928 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 |
4184 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 |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4140 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
3 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3928 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Incorrect |
3 ms |
4188 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
3928 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 |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4200 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
4 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 |
1 ms |
4188 KB |
Output is correct |
23 |
Incorrect |
2 ms |
4184 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |