-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBearAndBribingTree.java
More file actions
73 lines (64 loc) · 1.89 KB
/
BearAndBribingTree.java
File metadata and controls
73 lines (64 loc) · 1.89 KB
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
65
66
67
68
69
70
71
72
73
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Integer.max;
import static java.lang.Integer.min;
/**
* Created by hardCode on 2/25/2017.
*/
public class BearAndBribingTree {
final static int MAX = 1<<17;
static int[] a = new int[MAX];
static BufferedReader br;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int t,h,k,n;
String[] s;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {
s=br.readLine().split("\\s");
h=Integer.parseInt(s[0]);
k=Integer.parseInt(s[1]);
n=1<<h;
readArray(a, n);
System.out.println(solve(a,n,h,k));
}
}
private static int solve(int[] a, int n, int h, int k) {
int bribe = 0;
for (int i = 0; i < h; i++) {
int p=1;
if (a[1]>a[0]){
if (a[1]-a[0]>k)
return -1;
else bribe++;
}
for (int j =2; j <n-1 ; j+=2) {
int max=max(a[j],a[j+1]);
int min=min(a[j],a[j+1]);
if (max<a[0])
a[j-p]=max;
else if (max - min <= k) {
bribe++;
a[j-p]=min;
}
else a[j-p]=max;
p++;
}
n>>>=1;
for (int j = 0; j < n; j++) {
System.out.print(a[j] + " ");
}
System.out.println();
}
return bribe;
}
private static void readArray(int[] a, int n) throws IOException {
String[] s;
s = br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s[i]);
}
}
}