/*Code by TMQ*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define em emplace
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FIV(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define all(x) (x).begin(), (x).end()
#define reset(a, x) (memset(a, x, sizeof(a)));
#define testcase \
int tc; \
cin >> tc; \
while (tc--) \
solve();
#define howlong cerr << "Time elapsed: " << fixed << setprecision(9) << (double)clock() / CLOCKS_PER_SEC << "s";
#define what_is(x) cerr << #x << " is " << x << '\n';
ll inline GCD(ll a, ll b) { return !b ? abs(a) : GCD(b, a % b); }
ll inline LCM(ll a, ll b) { return (a / GCD(a, b) * b); }
template <class X, class Y>
bool maximize(X &x, Y y)
{
if (x < y)
{
x = y;
return true;
}
return false;
};
template <class X, class Y>
bool minimize(X &x, Y y)
{
if (x > y)
{
x = y;
return true;
}
return false;
};
///=====================================s========================================
#define BIT(i, mask) ((mask >> (i)) & 1)
#define DAOBIT(i, mask) ((mask ^ (1 << i)))
#define OFFBIT(i, mask) ((mask & ~(1 << (i))))
#define ONBIT(i, mask) ((mask | (1 << (i))))
///============================================================================
const ll mod = 1e9 + 7;
const ll inf = (ll)1e18 + 10;
const ll nmax = 5e4 + 5;
const ll B = 230;
///============================================================================
void TASK(string task)
{
if (fopen((task + ".inp").c_str(), "r"))
{
freopen((task + ".inp").c_str(), "r", stdin);
freopen((task + ".out").c_str(), "w", stdout);
}
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x)
{
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
{ \
cerr << "[" << #x << "] = ["; \
_print(x); \
}
#else
#define debug(x...)
#endif
///============================================================================
int n, m, b[nmax], p[nmax];
vector<vector<int>> doge;
bool check[nmax][B];
int dp[nmax];
///============================================================================
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
TASK("name");
cin >> n >> m;
doge.resize(n + 1);
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
doge[b[i]].push_back(p[i]);
if (p[i] < B)
{
check[b[i]][p[i]] = true;
}
}
reset(dp, 0x3f);
ll max_val = dp[0];
dp[b[0]] = 0;
priority_queue<ii, vector<ii>, greater<ii>> pq;
pq.push({0, b[0]});
while (!pq.empty())
{
int v = pq.top().second;
int d = pq.top().first;
pq.pop();
if (dp[v] != d)
continue;
if (v == b[1])
break;
for (int k = 0; k < doge[v].size(); k++)
{
int p = doge[v][k];
for (int i = v + p; i < n; i += p)
{
if (dp[i] > dp[v] + (i - v) / p)
{
dp[i] = dp[v] + (i - v) / p;
pq.push({dp[i], i});
}
if (p < B && check[i][p])
break;
if (p < B)
check[i][p] = 1;
}
for (int i = v - p; i >= 0; i -= p)
{
if (dp[i] > dp[v] + (v - i) / p)
{
dp[i] = dp[v] + (v - i) / p;
pq.push({dp[i], i});
}
if (p < B && check[i][p])
break;
if (p < B)
check[i][p] = 1;
}
}
}
cout << (dp[b[1]] == max_val ? -1 : dp[b[1]]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:152:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for (int k = 0; k < doge[v].size(); k++)
| ~~^~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'void TASK(std::string)':
skyscraper.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen((task + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen((task + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2808 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2556 KB |
Output is correct |
13 |
Correct |
1 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2808 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2808 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2640 KB |
Output is correct |
13 |
Correct |
1 ms |
2536 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
19 |
Correct |
1 ms |
2640 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2640 KB |
Output is correct |
22 |
Correct |
1 ms |
2640 KB |
Output is correct |
23 |
Correct |
1 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
1 ms |
2640 KB |
Output is correct |
28 |
Correct |
1 ms |
2640 KB |
Output is correct |
29 |
Correct |
2 ms |
2896 KB |
Output is correct |
30 |
Correct |
1 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2808 KB |
Output is correct |
32 |
Correct |
2 ms |
2896 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
2 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
1 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2812 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2808 KB |
Output is correct |
20 |
Correct |
1 ms |
2788 KB |
Output is correct |
21 |
Correct |
1 ms |
2640 KB |
Output is correct |
22 |
Correct |
1 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
1 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2812 KB |
Output is correct |
29 |
Correct |
3 ms |
2896 KB |
Output is correct |
30 |
Correct |
1 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
5 ms |
3152 KB |
Output is correct |
36 |
Correct |
2 ms |
2812 KB |
Output is correct |
37 |
Correct |
5 ms |
3152 KB |
Output is correct |
38 |
Correct |
5 ms |
3324 KB |
Output is correct |
39 |
Correct |
6 ms |
3164 KB |
Output is correct |
40 |
Correct |
6 ms |
3320 KB |
Output is correct |
41 |
Correct |
5 ms |
3152 KB |
Output is correct |
42 |
Correct |
4 ms |
3152 KB |
Output is correct |
43 |
Correct |
4 ms |
3152 KB |
Output is correct |
44 |
Correct |
4 ms |
3152 KB |
Output is correct |
45 |
Correct |
7 ms |
3508 KB |
Output is correct |
46 |
Correct |
7 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2808 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
1 ms |
2640 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
1 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2640 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
1 ms |
2640 KB |
Output is correct |
29 |
Correct |
2 ms |
2896 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
6 ms |
3152 KB |
Output is correct |
36 |
Correct |
2 ms |
2640 KB |
Output is correct |
37 |
Correct |
5 ms |
3152 KB |
Output is correct |
38 |
Correct |
6 ms |
3152 KB |
Output is correct |
39 |
Correct |
5 ms |
3152 KB |
Output is correct |
40 |
Correct |
5 ms |
3152 KB |
Output is correct |
41 |
Correct |
5 ms |
3152 KB |
Output is correct |
42 |
Correct |
5 ms |
3408 KB |
Output is correct |
43 |
Correct |
5 ms |
3152 KB |
Output is correct |
44 |
Correct |
5 ms |
3408 KB |
Output is correct |
45 |
Correct |
7 ms |
3600 KB |
Output is correct |
46 |
Correct |
7 ms |
3152 KB |
Output is correct |
47 |
Correct |
9 ms |
6092 KB |
Output is correct |
48 |
Correct |
7 ms |
8016 KB |
Output is correct |
49 |
Correct |
6 ms |
8016 KB |
Output is correct |
50 |
Correct |
8 ms |
9980 KB |
Output is correct |
51 |
Correct |
14 ms |
10956 KB |
Output is correct |
52 |
Correct |
17 ms |
10956 KB |
Output is correct |
53 |
Correct |
10 ms |
10576 KB |
Output is correct |
54 |
Correct |
6 ms |
8272 KB |
Output is correct |
55 |
Correct |
10 ms |
8272 KB |
Output is correct |
56 |
Correct |
13 ms |
9296 KB |
Output is correct |
57 |
Correct |
7 ms |
8540 KB |
Output is correct |
58 |
Correct |
10 ms |
8636 KB |
Output is correct |
59 |
Correct |
11 ms |
8528 KB |
Output is correct |
60 |
Correct |
12 ms |
8528 KB |
Output is correct |
61 |
Correct |
11 ms |
8528 KB |
Output is correct |
62 |
Correct |
13 ms |
9188 KB |
Output is correct |
63 |
Correct |
32 ms |
9848 KB |
Output is correct |
64 |
Correct |
31 ms |
9668 KB |
Output is correct |
65 |
Correct |
39 ms |
10696 KB |
Output is correct |
66 |
Correct |
52 ms |
12996 KB |
Output is correct |
67 |
Correct |
48 ms |
12996 KB |
Output is correct |