-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9489.cpp
64 lines (60 loc) · 1.39 KB
/
9489.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int MAX = 1e6 + 1;
bool solve()
{
int N, K;
cin >> N >> K;
if (N + K == 0)
return false;
int arr[N];
for (int i = 0; i < N; i++)
cin >> arr[i];
int parent[MAX];
int depth[MAX];
for (int i = 0; i < MAX; i++)
depth[i] = parent[i] = -1;
vector<vector<int>> nodes_by_depth(N + 1);
int prev = arr[0];
depth[arr[0]] = 0;
nodes_by_depth[0].push_back(arr[0]);
int cur_depth = 1;
int j = -1;
for (int i = 1; i < N; i++)
{
if (prev + 1 != arr[i])
j++;
if (nodes_by_depth[cur_depth - 1].size() == j)
cur_depth++, j = 0;
nodes_by_depth[cur_depth].push_back(arr[i]);
int p = nodes_by_depth[cur_depth - 1][j];
parent[arr[i]] = p;
depth[arr[i]] = cur_depth;
prev = arr[i];
}
int result = 0;
if (parent[K] > 0 && parent[parent[K]] > 0)
{
int gp = parent[parent[K]];
for (auto sibling : nodes_by_depth[depth[K]])
{
if (parent[K] == parent[sibling])
continue;
if (parent[parent[sibling]] == gp)
result++;
}
}
cout << result << "\n";
return true;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
}