#include <bits/stdc++.h>
using namespace std;
#define Task "skyscraper"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3e4 + 5;
const int oo = 0x3f3f3f3f, mod = 1e9 + 7;
int n, m, k;
string s;
vector<int> arr;
vector<int> ed[N];
vector<pair<int, int>> ej[N];
bool find(int j, int d) {
auto it = lower_bound(ed[j].begin(), ed[j].end(), d);
return it != ed[j].end() && *it == d;
}
void process() {
static int dd[N];
int n, m, i, j, d, s, t;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;
cin >> n >> m;
s = t = -1;
for (i = 0; i < m; i++) {
cin >> j >> d;
if (i == 0)
s = j;
else if (i == 1)
t = j;
ed[j].push_back(d);
}
for (i = 0; i < n; i++) {
sort(ed[i].begin(), ed[i].end());
ed[i].erase(unique(ed[i].begin(), ed[i].end()), ed[i].end());
}
for (i = 0; i < n; i++)
for (int d : ed[i]) {
for (j = i + d; j < n; j += d) {
ej[i].push_back({j, (j - i) / d});
if (find(j, d))
break;
}
for (j = i - d; j >= 0; j -= d) {
ej[i].push_back({j, (i - j) / d});
if (find(j, d))
break;
}
}
memset(dd, 0x3f, n * sizeof *dd);
dd[s] = 0;
pq.push({0, s});
while (!pq.empty()) {
i = pq.top().second;
pq.pop();
for (auto tmp : ej[i]) {
int j=tmp.fi, w=tmp.se;
d = dd[i] + w;
if (d < dd[j]) {
dd[j] = d;
pq.push({d, j});
}
}
}
cout << (dd[t] == oo ? -1 : dd[t]);
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
if(fopen(Task".INP", "r")) {
freopen(Task".INP", "r", stdin);
freopen(Task".OUT", "w", stdout);
}
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(Task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(Task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1624 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1872 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1940 KB |
Output is correct |
24 |
Correct |
2 ms |
1880 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2140 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3420 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1872 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
2 ms |
2136 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2140 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3416 KB |
Output is correct |
34 |
Correct |
7 ms |
3412 KB |
Output is correct |
35 |
Correct |
7 ms |
3160 KB |
Output is correct |
36 |
Correct |
2 ms |
2140 KB |
Output is correct |
37 |
Correct |
9 ms |
4088 KB |
Output is correct |
38 |
Correct |
10 ms |
3932 KB |
Output is correct |
39 |
Correct |
10 ms |
4080 KB |
Output is correct |
40 |
Correct |
10 ms |
3932 KB |
Output is correct |
41 |
Correct |
10 ms |
3932 KB |
Output is correct |
42 |
Correct |
4 ms |
2140 KB |
Output is correct |
43 |
Correct |
4 ms |
2396 KB |
Output is correct |
44 |
Correct |
4 ms |
2140 KB |
Output is correct |
45 |
Correct |
14 ms |
6748 KB |
Output is correct |
46 |
Correct |
15 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
2 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1864 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1892 KB |
Output is correct |
19 |
Correct |
1 ms |
2136 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
2 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
3 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
3 ms |
2252 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3416 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
35 |
Correct |
8 ms |
3164 KB |
Output is correct |
36 |
Correct |
2 ms |
2140 KB |
Output is correct |
37 |
Correct |
9 ms |
4188 KB |
Output is correct |
38 |
Correct |
10 ms |
3932 KB |
Output is correct |
39 |
Correct |
10 ms |
3932 KB |
Output is correct |
40 |
Correct |
10 ms |
3932 KB |
Output is correct |
41 |
Correct |
10 ms |
3928 KB |
Output is correct |
42 |
Correct |
4 ms |
2136 KB |
Output is correct |
43 |
Correct |
4 ms |
2140 KB |
Output is correct |
44 |
Correct |
4 ms |
2140 KB |
Output is correct |
45 |
Correct |
14 ms |
6748 KB |
Output is correct |
46 |
Correct |
14 ms |
6748 KB |
Output is correct |
47 |
Correct |
32 ms |
11472 KB |
Output is correct |
48 |
Correct |
13 ms |
5044 KB |
Output is correct |
49 |
Correct |
9 ms |
3932 KB |
Output is correct |
50 |
Correct |
7 ms |
3932 KB |
Output is correct |
51 |
Correct |
28 ms |
6872 KB |
Output is correct |
52 |
Correct |
29 ms |
7508 KB |
Output is correct |
53 |
Correct |
12 ms |
4312 KB |
Output is correct |
54 |
Correct |
3 ms |
2396 KB |
Output is correct |
55 |
Correct |
4 ms |
2776 KB |
Output is correct |
56 |
Correct |
11 ms |
3932 KB |
Output is correct |
57 |
Correct |
31 ms |
11060 KB |
Output is correct |
58 |
Correct |
7 ms |
3028 KB |
Output is correct |
59 |
Correct |
8 ms |
3544 KB |
Output is correct |
60 |
Correct |
9 ms |
4312 KB |
Output is correct |
61 |
Correct |
8 ms |
3620 KB |
Output is correct |
62 |
Correct |
15 ms |
8156 KB |
Output is correct |
63 |
Correct |
53 ms |
24812 KB |
Output is correct |
64 |
Correct |
64 ms |
28108 KB |
Output is correct |
65 |
Correct |
83 ms |
37580 KB |
Output is correct |
66 |
Correct |
180 ms |
67128 KB |
Output is correct |
67 |
Correct |
184 ms |
67388 KB |
Output is correct |