-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17071.cpp
46 lines (43 loc) · 1.23 KB
/
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
45
46
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int MAX = 1e5*5+1;
const int MAX_VALUE = 1e5*5;
int first_visited[MAX];
int visited[MAX][2];
void solve()
{
int N, K; cin >> N >> K;
for (int i = 0; i < MAX; i++) first_visited[i] = -1;
for (int i = 0; i + K <= MAX_VALUE; i++) {
K += i;
first_visited[K] = i;
}
queue<pair<int, int>> q;
int result = 9999999;
q.push({N, 0});
while (q.size()) {
auto [x, count] = q.front(); q.pop();
if (first_visited[x] > -1) {
if (first_visited[x] >= count && (first_visited[x] - count) % 2 == 0) result = min(result, first_visited[x]);
}
int xxx[] = {x*2, x+1, x-1};
for (int i = 0; i < 3; i++) {
int next = xxx[i];
int next_count = count + 1;
int flag = next_count % 2;
if (next < 0 || next > MAX_VALUE) continue;
if (visited[next][flag]) continue;
visited[next][flag] = next_count;
q.push({next, next_count});
}
}
cout << (result == 9999999 ? -1 : result);
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}