forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17071.cpp
44 lines (42 loc) · 982 Bytes
/
17071.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Authored by : heheHwang
// Co-authored by : -
// http://boj.kr/884a292d995243cd92d78c1f1dde977b
#include <bits/stdc++.h>
using namespace std;
const int LMT = 500'000;
int board[LMT + 2];
int vis[2][LMT + 2];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int bro, sis, ans = 0;
queue<pair<int, int>> Q;
cin >> sis >> bro;
Q.push({sis, 0});
for (int i = 0; i < 2; i++)
fill(vis[i], vis[i] + LMT + 2, -1);
vis[0][sis] = 0;
while (!Q.empty()) {
int v, vt, nvt;
tie(v, vt) = Q.front();
nvt = vt + 1;
Q.pop();
for (int nv : {2 * v, v + 1, v - 1}) {
if (nv < 0 || LMT < nv) continue;
if (vis[nvt % 2][nv] != -1) continue;
vis[nvt % 2][nv] = nvt;
Q.push({nv, nvt});
}
}
bool found = false;
while (bro <= LMT) {
if (vis[ans % 2][bro] <= ans) {
found = true;
break;
}
bro += ++ans;
}
if (found) cout << ans;
else
cout << -1;
}