#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll n, m;
cin >> n >> m;
ll b[m], p[m];
vector<ll> eq[n];
for (ll i = 0; i < m; i++)
cin >> b[i] >> p[i], eq[b[i]].push_back(i);
vector<ll> g[m];
for (ll i = 0; i < n; i++)
for (ll j = 1; j < eq[i].size(); j++)
g[eq[i][j]].push_back(eq[i][0]), g[eq[i][0]].push_back(eq[i][j]);
map<ll, ll> dist[m];
dist[0][b[0]] = 0;
deque<pair<ll, ll>> q;
q.push_front(make_pair(0, b[0]));
while (!q.empty())
{
auto [i, j] = q.front();
q.pop_front();
if (j - p[i] >= 0 and (!dist[i].count(j - p[i]) or dist[i][j - p[i]] > dist[i][j] + 1))
dist[i][j - p[i]] = dist[i][j] + 1, q.push_back(make_pair(i, j - p[i]));
if (j + p[i] < n and (!dist[i].count(j + p[i]) or dist[i][j + p[i]] > dist[i][j] + 1))
dist[i][j + p[i]] = dist[i][j] + 1, q.push_back(make_pair(i, j + p[i]));
for (ll k : eq[j])
if (!dist[k].count(j) or dist[k][j] > dist[i][j])
dist[k][j] = dist[i][j], q.push_front(make_pair(k, j));
eq[j].clear();
}
ll best = 1e18;
for (auto i : dist[1])
best = min(best, i.second);
if (best == 1e18)
best = -1;
cout << best << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll i = 1; i <= t; i++)
solve();
cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:16:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (ll j = 1; j < eq[i].size(); j++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
456 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
27 ms |
13148 KB |
Output is correct |
13 |
Correct |
25 ms |
13148 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
25 ms |
13148 KB |
Output is correct |
13 |
Correct |
25 ms |
13148 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
1016 ms |
167508 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
26 ms |
13136 KB |
Output is correct |
13 |
Correct |
24 ms |
13144 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1049 ms |
170844 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
28 ms |
12884 KB |
Output is correct |
13 |
Correct |
28 ms |
13148 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1057 ms |
169288 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |