-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapy.java
More file actions
39 lines (39 loc) · 1.42 KB
/
Heapy.java
File metadata and controls
39 lines (39 loc) · 1.42 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
import java.util.*;
import java.io.*;
public class Heapy {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
int[] a = new int[n + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
boolean poss = true;
boolean[] vis = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
List<Integer> pos = new ArrayList<>();
List<Integer> val = new ArrayList<>();
int curr = i;
while (curr <= n) {
vis[curr] = true;
pos.add(curr);
val.add(a[curr]);
if (curr > n / 2) break;
curr *= 2;
}
Collections.sort(pos);
Collections.sort(val);
if (!pos.equals(val)) {
poss = false;
break;
}
}
}
System.out.println(poss ? "YES" : "NO");
}
}
}