#P7805. Bookworm Fight Deduction

    ID: 20991 Type: Default 1000ms 256MiB

Bookworm Fight Deduction

Bookworm Fight Deduction

You have N bookworms labeled from 0 to N-1. Each bookworm i has a unique strength Si in the range \( [0, N-1] \). You do not know these strengths. Your only way to learn about the hidden strengths is to call a "fight event" (via the function Query(a, b)) up to 25000 times.

The fight event is defined as follows. When you choose two different bookworms with indices a and b:

  • If \(|S_a-S_b|=1\) (i.e. their strength values are consecutive) then the bookworm with the smaller strength wins.
  • If \(|S_a-S_b|>1\) then the bookworm with the larger strength wins.

Your task is to determine the hidden strength of every bookworm with as few fight events as possible. In other words, implement the function:

[ \texttt{std::vector Solve(int N)} ]

which returns an array \(T\) of length N satisfying:

  • \(T_i\in[0, N-1]\) for all \(i\) (each value appears exactly once);
  • \(T_i = S_i\) for all \(i\).

You are allowed to call the function:

[ \texttt{bool Query(int a, int b)} ]

which simulates a fight between bookworms a and b (with \(a \neq b\)). When you call Query(a, b):

  • If \(|S_a-S_b|=1\), then the bookworm with the smaller strength wins, so the function returns true if \(S_a<S_b\), and false otherwise.
  • If \(|S_a-S_b|>1\), then the bookworm with the larger strength wins, so the function returns true if \(S_a>S_b\), and false otherwise.

Your program must not use standard input or output when deployed, though for testing you may simulate the judge using an input file. You may call Query at most 25000 times; otherwise your solution will be judged Wrong Answer [6].

Special Notes:

  1. You may use functions from the standard library and global variables.
  2. Your submission must implement the function Solve and may call Query as described.

inputFormat

The input is provided by the interactive judge for simulation. For offline testing the input contains an integer N on the first line followed by N space‐separated integers representing the hidden permutation \(S\) of \(\{0,1,\ldots,N-1\}\).

outputFormat

Your function should return an array T of length N such that \(T_i=S_i\). For offline simulation, output the N numbers separated by spaces.

sample

4
0 1 2 3
0 1 2 3

</p>