This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Arayi
#include <bits/stdc++.h>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
char dc[] = {'R', 'D', 'L', 'U'};
const int N = 2e6 + 20;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = 1e-13;
const int T = 1;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1)
ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream &operator<<(ostream &c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template <class T>
void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
int n, m;
int b[N], p1[N];
int d[N][T];
vii fp[N];
int main()
{
fastio;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p1[i];
fp[b[i]].ad(i);
d[i][0] = mod;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < T; j++)
{
d[i][j] = mod;
}
}
priority_queue<pair<int, pair<int, int>>> q;
q.push(MP(0, MP(0, 0)));
if (p1[0] < T)
q.push(MP(0, MP(b[0], p1[0])));
while (!q.empty())
{
auto p = q.top();
q.pop();
if (d[p.sc.fr][p.sc.sc] < mod)
continue;
d[p.sc.fr][p.sc.sc] = -p.fr;
// cout << p.fr << " " << p.sc << endl;
if (p.sc.sc == 0)
{
if (p1[p.sc.sc] < T)
continue;
for (int i = b[p.sc.fr]; i < n; i += p1[p.sc.fr])
{
for (auto i1 : fp[i])
{
if (d[i1][0] < mod)
continue;
if (p1[i1] < T && d[i][p1[i1]] == mod)
q.push(MP(p.fr - ((i - b[p.sc.fr]) / p1[p.sc.fr]), MP(i, p1[i1])));
q.push(MP(p.fr - ((i - b[p.sc.fr]) / p1[p.sc.fr]), MP(i1, 0)));
}
}
for (int i = b[p.sc.fr] - p1[p.sc.fr]; i >= 0; i -= p1[p.sc.fr])
{
for (auto i1 : fp[i])
{
if (d[i1][0] < mod)
continue;
if (p1[i1] < T && d[i][p1[i1]] == mod)
q.push(MP(p.fr - ((b[p.sc.fr] - i) / p1[p.sc.fr]), MP(i, p1[i1])));
q.push(MP(p.fr - ((b[p.sc.fr] - i) / p1[p.sc.fr]), MP(i1, 0)));
}
}
}
else
{
for (auto i1 : fp[p.sc.fr])
{
if (d[i1][0] < mod)
continue;
q.push(MP(p.fr, MP(i1, 0)));
if (p1[i1] < T && d[p.sc.fr][p1[i1]] == mod)
q.push(MP(p.fr, MP(p.sc.fr, p1[i1])));
}
if (p.sc.fr + p.sc.sc < n && d[p.sc.fr + p.sc.sc][p.sc.sc] == mod)
q.push(MP(p.fr - 1, MP(p.sc.fr + p.sc.sc, p.sc.sc)));
if (p.sc.fr - p.sc.sc >= 0 && d[p.sc.fr - p.sc.sc][p.sc.sc] == mod)
q.push(MP(p.fr - 1, MP(p.sc.fr - p.sc.sc, p.sc.sc)));
}
}
if (d[1][0] == mod)
cout << -1 << endl;
else
cout << d[1][0] << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:149:70: warning: array subscript [0, 0] is outside array bounds of 'int [1]' [-Warray-bounds]
149 | if (p.sc.fr + p.sc.sc < n && d[p.sc.fr + p.sc.sc][p.sc.sc] == mod)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
skyscraper.cpp:151:71: warning: array subscript [0, 0] is outside array bounds of 'int [1]' [-Warray-bounds]
151 | if (p.sc.fr - p.sc.sc >= 0 && d[p.sc.fr - p.sc.sc][p.sc.sc] == mod)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
# | 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... |