BinSearch

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

bool binary_search ( int n , int p [] , int target ){
    int left = 1 , right = n ;
    while ( left < right ){
        int mid = ( left + right ) / 2;
        if( p [ mid ] == target )
            return true ;
        else if( p[ mid ] < target )
            left = mid + 1;
        else
            right = mid - 1;
    }
    if( p [ left ] == target ) return true ;
    else return false ;
}

It is well known that if p happens to be sorted, then this code returns true if and only if target appears within p. On the other hand, this may not be the case if p is not sorted.

You are given a positive integer nn and a sequence b\_1, \dots , b\_n ∈ \\{true, false\\}. It is guaranteed that n=2k1n = 2^{k} - 1 for some positive integer kk. You must generate a permutation pp of 1,,n\\{1, \dots , n\\} that follows certain conditions. Let S(p)S(p) be the number of indices i1,,ni \in \\{1, \dots , n\\} for which binary_search(n, p, i) does not return b_ib\_i. You must set pp so that S(p)S(p) is small (as detailed in the “Restrictions” section).

Note: a permutation of 1,,n\\{1, \dots , n\\} is a sequence of nn integers that contains each integer from 11 to nn exactly once.

입력

The input contains multiple test cases. The first line of input contains TT, the number of test cases. The test cases follow.

The first line of a test case contains the integer nn. The second line of a test case contains a string of length n containing only characters '0' and '1'. These characters are not separated by spaces. If the iith character is '1', then b_i=b\_i = true, and if it is '0', then b_i=b\_i = false.

출력

The output data consists of the answers for each of the TT test cases. The answer for a particular test case consists of the permutation pp generated for that test case.

제한

  • Let n\sum{n} be the sum of all values of nn in a single input.
  • 1n100,0001 ≤ \sum{n} ≤ 100\\,000.
  • 1T7,0001 ≤ T ≤ 7\\,000.
  • n=2k1n = 2^{k} - 1 for some kNk \in \mathbb{N}, k>0k > 0.
  • If S(p)1S(p) ≤ 1 for all test cases within a subtask, then you are given 100100\\% of the points for that subtask.
  • Otherwise, if 0S(p)log_2n0 ≤ S(p) ≤ \lceil log\_{2}{n} \rceil (i.e. 12S(p)n+11 ≤ 2^{S(p)} ≤ n + 1) for all test cases within a subtask, then you are given 5050\\% of the points for that subtask.

힌트

Example 1. In the first two test cases of the first example, we have S(p)=0S(p) = 0.

In the third test case, we have S(p)=1S(p) = 1. This is because binary_search(n, p, 2) returns true, although b_2=b\_2 = false.

In the forth test case, we have S(p)=1S(p) = 1. This is because binary_search(n, p, 4) returns true, although b_4=b\_4 = false.

Example 2. We have S(p)=0S(p) = 0 for both test cases.