#include<bits/stdc++.h>
#define taskname "skyscraper"
using namespace std;
typedef long long ll;
template<class T>bool minimize(T& a, T b){
if(a > b){
a = b;
return true;
}
return false;
}
const int lim = 3e4 + 5;
int n, m, b[lim], p[lim];
namespace sub1{
void solve(){
if(m == 2){
return void(cout << (abs(b[0] - b[1]) % p[0] == 0 ? abs(b[0] - b[1]) / p[0] : -1));
}
int ans = (abs(b[0] - b[1]) % p[0] == 0 ? abs(b[0] - b[1]) / p[0] : INT_MAX);
if(abs(b[0] - b[2]) % p[0] == 0 && abs(b[2] - b[1]) % p[2] == 0){
minimize(ans, abs(b[0] - b[2]) / p[0] + abs(b[2] - b[1]) / p[2]);
}
cout << (ans == INT_MAX ? -1 : ans);
}
}
namespace sub23{
const int LIM = 2e3 + 5;
int d[LIM];
void solve(){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
memset(d, 0x0f, sizeof(d));
pq.emplace(d[0] = 0, 0);
while(!pq.empty()){
auto [w, u] = pq.top();
pq.pop();
if(w == d[u]){
for(int v = 0; v < m; v++){
if(abs(b[u] - b[v]) % p[u] == 0 && minimize(d[v], w + abs(b[u] - b[v]) / p[u])){
pq.emplace(d[v], v);
}
}
}
}
cout << (d[1] == d[LIM - 1] ? -1 : d[1]);
}
}
namespace sub4{
const int LIM = 2e3 + 5;
int d[LIM];
vector<int>pos[LIM];
void solve(){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
memset(d, 0x0f, sizeof(d));
pq.emplace(d[b[0]] = 0, b[0]);
for(int i = 0; i < m; i++){
pos[b[i]].emplace_back(i);
}
while(!pq.empty()){
auto [w, u] = pq.top();
pq.pop();
if(u == b[1]){
return void(cout << w);
}
if(w == d[u]){
for(int& index : pos[u]){
for(int v = u - p[index], cnt = 1; v > -1; v -= p[index], cnt++){
if(minimize(d[v], w + cnt)){
pq.emplace(d[v], v);
}
}
for(int v = u + p[index], cnt = 1; v < n; v += p[index], cnt++){
if(minimize(d[v], w + cnt)){
pq.emplace(d[v], v);
}
}
}
}
}
cout << -1;
}
}
namespace sub5{
const int SIZE = 50;
vector<int>pos[lim];
vector<vector<int>>e[SIZE];
int d[lim];
void solve(){
for(int i = 1; i < SIZE; i++){
e[i].resize(i);
}
for(int i = 0; i < m; i++){
pos[b[i]].emplace_back(i);
for(int j = 1; j < SIZE; j++){
e[j][b[i] % j].emplace_back(b[i]);
}
}
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < i; j++){
sort(e[i][j].begin(), e[i][j].end());
e[i][j].resize(unique(e[i][j].begin(), e[i][j].end()) - e[i][j].begin());
}
}
for(int i = 0; i < n; i++){
sort(pos[i].begin(), pos[i].end());
pos[i].resize(unique(pos[i].begin(), pos[i].end()) - pos[i].begin());
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
memset(d, 0x0f, sizeof(d));
pq.emplace(d[b[0]] = 0, b[0]);
while(!pq.empty()){
auto [w, u] = pq.top();
pq.pop();
if(u == b[1]){
return void(cout << w);
}
if(w == d[u]){
for(int& index : pos[u]){
if(p[index] < SIZE){
for(int& v : e[p[index]][u % p[index]]){
if(minimize(d[v], w + abs(v - u) / p[index])){
pq.emplace(d[v], v);
}
}
}
else{
for(int v = u - p[index], cnt = 1; v > -1; v -= p[index], cnt++){
if(minimize(d[v], w + cnt)){
pq.emplace(d[v], v);
}
}
for(int v = u + p[index], cnt = 1; v < n; v += p[index], cnt++){
if(minimize(d[v], w + cnt)){
pq.emplace(d[v], v);
}
}
}
}
}
}
cout << -1;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
}
if(n <= 10 && m <= 3){
sub1::solve();
}
else if(n <= 2000 && m <= 2000){
sub23::solve();
}
else if(n <= 2000){
sub5::solve();
}
else{
sub5::solve();
}
}
Compilation message
skyscraper.cpp: In function 'void sub23::solve()':
skyscraper.cpp:34:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto [w, u] = pq.top();
| ^
skyscraper.cpp: In function 'void sub4::solve()':
skyscraper.cpp:59:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | auto [w, u] = pq.top();
| ^
skyscraper.cpp: In function 'void sub5::solve()':
skyscraper.cpp:111:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
111 | auto [w, u] = pq.top();
| ^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:146:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
20 ms |
1312 KB |
Output is correct |
12 |
Correct |
18 ms |
1252 KB |
Output is correct |
13 |
Correct |
18 ms |
1116 KB |
Output is correct |
14 |
Correct |
20 ms |
1364 KB |
Output is correct |
15 |
Correct |
20 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1252 KB |
Output is correct |
11 |
Correct |
20 ms |
1308 KB |
Output is correct |
12 |
Correct |
18 ms |
1116 KB |
Output is correct |
13 |
Correct |
18 ms |
1116 KB |
Output is correct |
14 |
Correct |
20 ms |
1116 KB |
Output is correct |
15 |
Correct |
20 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
16 ms |
1312 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
23 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1112 KB |
Output is correct |
24 |
Correct |
17 ms |
1112 KB |
Output is correct |
25 |
Correct |
18 ms |
1112 KB |
Output is correct |
26 |
Correct |
20 ms |
1368 KB |
Output is correct |
27 |
Correct |
18 ms |
1116 KB |
Output is correct |
28 |
Correct |
19 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
20 ms |
1360 KB |
Output is correct |
34 |
Correct |
20 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1036 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
22 ms |
1312 KB |
Output is correct |
12 |
Correct |
18 ms |
1116 KB |
Output is correct |
13 |
Correct |
18 ms |
1116 KB |
Output is correct |
14 |
Correct |
20 ms |
1116 KB |
Output is correct |
15 |
Correct |
20 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
15 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
23 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
17 ms |
1280 KB |
Output is correct |
25 |
Correct |
18 ms |
1112 KB |
Output is correct |
26 |
Correct |
19 ms |
1288 KB |
Output is correct |
27 |
Correct |
19 ms |
1368 KB |
Output is correct |
28 |
Correct |
19 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
20 ms |
1116 KB |
Output is correct |
34 |
Correct |
21 ms |
1360 KB |
Output is correct |
35 |
Correct |
58 ms |
9308 KB |
Output is correct |
36 |
Correct |
11 ms |
2904 KB |
Output is correct |
37 |
Correct |
46 ms |
7420 KB |
Output is correct |
38 |
Correct |
82 ms |
10396 KB |
Output is correct |
39 |
Correct |
68 ms |
10324 KB |
Output is correct |
40 |
Correct |
72 ms |
10324 KB |
Output is correct |
41 |
Correct |
68 ms |
10460 KB |
Output is correct |
42 |
Correct |
31 ms |
8216 KB |
Output is correct |
43 |
Correct |
34 ms |
8160 KB |
Output is correct |
44 |
Correct |
370 ms |
8564 KB |
Output is correct |
45 |
Correct |
49 ms |
10320 KB |
Output is correct |
46 |
Correct |
49 ms |
10328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1204 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
20 ms |
1116 KB |
Output is correct |
12 |
Correct |
18 ms |
1112 KB |
Output is correct |
13 |
Correct |
18 ms |
1112 KB |
Output is correct |
14 |
Correct |
20 ms |
1116 KB |
Output is correct |
15 |
Correct |
20 ms |
1360 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
16 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1160 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
23 ms |
1260 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
5 ms |
1256 KB |
Output is correct |
24 |
Correct |
17 ms |
1112 KB |
Output is correct |
25 |
Correct |
19 ms |
1308 KB |
Output is correct |
26 |
Correct |
19 ms |
1368 KB |
Output is correct |
27 |
Correct |
19 ms |
1116 KB |
Output is correct |
28 |
Correct |
19 ms |
1112 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1112 KB |
Output is correct |
31 |
Correct |
2 ms |
1264 KB |
Output is correct |
32 |
Correct |
2 ms |
1112 KB |
Output is correct |
33 |
Correct |
20 ms |
1112 KB |
Output is correct |
34 |
Correct |
20 ms |
1112 KB |
Output is correct |
35 |
Correct |
57 ms |
9620 KB |
Output is correct |
36 |
Correct |
12 ms |
2908 KB |
Output is correct |
37 |
Correct |
46 ms |
7508 KB |
Output is correct |
38 |
Correct |
69 ms |
10404 KB |
Output is correct |
39 |
Correct |
67 ms |
10332 KB |
Output is correct |
40 |
Correct |
68 ms |
10296 KB |
Output is correct |
41 |
Correct |
69 ms |
10460 KB |
Output is correct |
42 |
Correct |
31 ms |
8216 KB |
Output is correct |
43 |
Correct |
38 ms |
8212 KB |
Output is correct |
44 |
Correct |
370 ms |
8528 KB |
Output is correct |
45 |
Correct |
48 ms |
10576 KB |
Output is correct |
46 |
Correct |
49 ms |
10328 KB |
Output is correct |
47 |
Correct |
76 ms |
9828 KB |
Output is correct |
48 |
Correct |
63 ms |
8672 KB |
Output is correct |
49 |
Correct |
56 ms |
8024 KB |
Output is correct |
50 |
Correct |
38 ms |
6156 KB |
Output is correct |
51 |
Correct |
89 ms |
11472 KB |
Output is correct |
52 |
Correct |
91 ms |
11396 KB |
Output is correct |
53 |
Correct |
84 ms |
11052 KB |
Output is correct |
54 |
Correct |
1 ms |
1116 KB |
Output is correct |
55 |
Correct |
1 ms |
1372 KB |
Output is correct |
56 |
Execution timed out |
1085 ms |
11384 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |