PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000144. Các điểm trên mặt phẳng (Points on Plane)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các điểm trên mặt phẳng (Points on Plane)

Points on Plane

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn pairwise distinct integer-coordinate points in the plane, numbered 1,…,n1,\ldots,n. Point ii has coordinates (xi,yi)(x_i,y_i).

The Manhattan distance between points ii and jj is ∣xi−xj∣+∣yi−yj∣|x_i-x_j|+|y_i-y_j|.

Find any permutation p1,…,pnp_1,\ldots,p_n of 1,…,n1,\ldots,n such that the total Manhattan distance between consecutive points is at most 2 500 000 0002\,500\,000\,000:

$$\sum_{i=1}^{n-1}\left(|x_{p_i}-x_{p_{i+1}}|+|y_{p_i}-y_{p_{i+1}}|\right)\le2\,500\,000\,000.$$

You do not need to minimize the length. A valid permutation is guaranteed to exist, and any valid permutation is accepted.

Input

  • The first line contains integer nn.
  • Each of the next nn lines contains xi,yix_i,y_i, the coordinates of point ii.

Output

Print a permutation of the integers 1,…,n1,\ldots,n whose consecutive-point Manhattan distance sum satisfies the upper bound.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le 100; integer coordinates 0≤xi,yi≤1060\le x_i,y_i\le10^6; all points distinct; total length at most 2 500 000 0002\,500\,000\,000.
  • Subtask 2 (30 points): 1≤n≤500001\le n\le 50000; integer coordinates 0≤xi,yi≤1060\le x_i,y_i\le10^6; all points distinct; total length at most 2 500 000 0002\,500\,000\,000.
  • Subtask 3 (50 points): 1≤n≤10000001\le n\le 1000000; integer coordinates 0≤xi,yi≤1060\le x_i,y_i\le10^6; all points distinct; total length at most 2 500 000 0002\,500\,000\,000.

Examples

Example 1

Input

5
0 7
8 10
3 4
5 0
9 12

Output

4 3 1 2 5

Explanation

The sample order is 4,3,1,2,54,3,1,2,5. The consecutive distances are 66, 66, 1111, and 33; total 2626, below 2 500 000 0002\,500\,000\,000. Every index appears exactly once. Other permutations are also valid.

A different valid permutation is also accepted.