This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++)
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |