-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructing the Array
More file actions
49 lines (40 loc) · 1.36 KB
/
Constructing the Array
File metadata and controls
49 lines (40 loc) · 1.36 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
import java.io.*;
import java.util.*;
public class Main {
static class Seg {
int l, r;
Seg(int l, int r) { this.l = l; this.r = r; }
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
int[] a = new int[n + 1];
PriorityQueue<Seg> pq = new PriorityQueue<>(
(x, y) -> {
int len1 = x.r - x.l;
int len2 = y.r - y.l;
if (len1 != len2) return len2 - len1;
return x.l - y.l;
}
);
pq.add(new Seg(1, n));
int cur = 1;
while (!pq.isEmpty()) {
Seg s = pq.poll();
int l = s.l, r = s.r;
if (l > r) continue;
int mid = (l + r) / 2;
a[mid] = cur++;
pq.add(new Seg(l, mid - 1));
pq.add(new Seg(mid + 1, r));
}
for (int i = 1; i <= n; i++) {
out.append(a[i]).append(i == n ? '\n' : ' ');
}
}
System.out.print(out);
}
}