click below for solution

Sunday, 30 January 2022

Roof Construction solution|Codeforces #769 (div 2)

                                                  B. Roof Construction

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It has finally been decided to build a roof over the football field in School 179. Its construction will require placing n consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation p of integers from 0 to n1, where pi is the height of the i-th pillar from the left (1in).

As the chief, you know that the cost of construction of consecutive pillars is equal to the maximum value of the bitwise XOR of heights of all pairs of adjacent pillars. In other words, the cost of construction is equal to max1in1pipi+1, where  denotes the bitwise XOR operation.

Find any sequence of pillar heights p of length n with the smallest construction cost.

In this problem, a permutation is an array consisting of n distinct integers from 0 to n1 in arbitrary order. For example, [2,3,1,0,4] is a permutation, but [1,0,1] is not a permutation (1 appears twice in the array) and [1,0,3] is also not a permutation (n=3, but 3 is in the array).

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t104). Description of the test cases follows.

The only line for each test case contains a single integer n (2n2105) — the number of pillars for the construction of the roof.

It is guaranteed that the sum of n over all test cases does not exceed 2105.

Output

For each test case print n integers p1p2pn — the sequence of pillar heights with the smallest construction cost.

If there are multiple answers, print any of them.

Note

For n=2 there are 2 sequences of pillar heights:

  • [0,1] — cost of construction is 01=1.
  • [1,0] — cost of construction is 10=1.

For n=3 there are 6 sequences of pillar heights:

  • [0,1,2] — cost of construction is max(01,12)=max(1,3)=3.
  • [0,2,1] — cost of construction is max(02,21)=max(2,3)=3.
  • [1,0,2] — cost of construction is max(10,02)=max(1,2)=2.
  • [1,2,0] — cost of construction is max(12,20)=max(3,2)=3.
  • [2,0,1] — cost of construction is max(20,01)=max(2,1)=2.
  • [2,1,0] — cost of construction is max(21,10)=max(3,1)=3.

Solution link below:

if 1st link is not work then click on another link

solution link 1


Solution link 2


Solution link 3

2 comments: