CF BUDDY
← Problems·

1254C · Point Ordering

2300 · constructive algorithms, geometry, interactive

Problem: This is an interactive problem.

Khanh has nn points on the Cartesian plane, denoted by a1,a2,,ana_1, a_2, \ldots, a_n. All points' coordinates are integers between 109-10^9 and 10910^9, inclusive. No three points are collinear. He says that these points are vertices of a convex polygon; in other words, there exists a permutation p1,p2,,pnp_1, p_2, \ldots, p_n of integers from 11 to nn such that the polygon ap1ap2apna_{p_1} a_{p_2} \ldots a_{p_n} is convex and vertices are listed in counter-clockwise order.

Khanh gives you the number nn, but hides the coordinates of his points. Your task is to guess the above permutation by asking multiple queries. In each query, you give Khanh 44 integers tt, ii, jj, kk; where either t=1t = 1 or t=2t = 2; and ii, jj, kk are three distinct indices from 11 to nn, inclusive. In response, Khanh tells you:

  • if t=1t = 1, the area of the triangle aiajaka_ia_ja_k multiplied by 22.
  • if t=2t = 2, the sign of the cross product of two vectors aiaj\overrightarrow{a_ia_j} and aiak\overrightarrow{a_ia_k}.

Recall that the cross product of vector a=(xa,ya)\overrightarrow{a} = (x_a, y_a) and vector b=(xb,yb)\overrightarrow{b} = (x_b, y_b) is the integer xaybxbyax_a \cdot y_b - x_b \cdot y_a. The sign of a number is 11 it it is positive, and 1-1 otherwise. It can be proven that the cross product obtained in the above queries can not be 00.

You can ask at most 3n3 \cdot n queries.

Please note that Khanh fixes the coordinates of his points and does not change it while answering your queries. You do not need to guess the coordinates. In your permutation ap1ap2apna_{p_1}a_{p_2}\ldots a_{p_n}, p1p_1 should be equal to 11 and the indices of vertices should be listed in counter-clockwise order.

Note: The image below shows the hidden polygon in the example:

The interaction in the example goes as below:

  • Contestant reads n=6n = 6.
  • Contestant asks a query with t=1t = 1, i=1i = 1, j=4j = 4, k=6k = 6.
  • Jury answers 1515. The area of the triangle A1A4A6A_1A_4A_6 is 7.57.5. Note that the answer is two times the area of the triangle.
  • Contestant asks a query with t=2t = 2, i=1i = 1, j=5j = 5, k=6k = 6.
  • Jury answers 1-1. The cross product of A1A5=(2,2)\overrightarrow{A_1A_5} = (2, 2) and A1A6=(4,1)\overrightarrow{A_1A_6} = (4, 1) is 2-2. The sign of 2-2 is 1-1.
  • Contestant asks a query with t=2t = 2, i=2i = 2, j=1j = 1, k=4k = 4.
  • Jury answers 11. The cross product of A2A1=(5,2)\overrightarrow{A_2A_1} = (-5, 2) and A2A4=(2,1)\overrightarrow{A_2A_4} = (-2, -1) is 11. The sign of 11 is 11.
  • Contestant says that the permutation is (1,3,4,2,6,5)(1, 3, 4, 2, 6, 5).

Sample Cases

Case 1

Input

6

15

-1

1

Output

1 1 4 6

2 1 5 6

2 2 1 4

0 1 3 4 2 6 5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback