#P9397. 「DBOI」Round 1 DTTM

「DBOI」Round 1 DTTM

Background

Zhang Zeyu and Mu Zhicheng sit on the rooftop, looking at the star-filled sky. In their world, connecting stars is a popular activity. They give it a romantic meaning: if you cannot connect them all, then the one remaining star is the person beside you; if you can connect them all, then both you and the person beside you are stars.

Problem Description

There are nn stars in the sky. The ii-th star is located at coordinates (xi,yi)(x_i, y_i). You need to connect the stars to satisfy Zhang Zeyu's requirements below:

  • Each star is an endpoint of exactly one line segment, and all segments do not intersect each other (including at endpoints).
  • The sum of xlxr|x_l - x_r| over all segments, where xlx_l and xrx_r are the xx-coordinates of the left and right endpoints, is minimized.

However, Zhang Zeyu is a bit slow and does not know how to connect them. Mu Zhicheng knows you are the smartest person on Earth, so he gives you the coordinates of the nn stars. You need to output a connection plan or report that there is no solution.

Input Format

The first line contains nn, the number of stars.

Starting from the second line, there are nn lines, each with two integers. Line i+1i+1 gives the coordinates (xi,yi)(x_i, y_i) of the ii-th star.

Output Format

On the first line, output the minimum possible value of the sum of xlxr|x_l - x_r| over all segments.

Then, on each following line, output two indices, indicating the pair of stars connected by each segment to achieve the minimum value.

If there are multiple valid connection plans, output any one of them. If there is no solution, output 1-1 on the first line.

4
1 3
2 2
2 1
3 4
2
1 4
2 3
6
1 5
2 3
2 4
2 5
2 -1
3 -3
2
1 3
4 6
2 5

Hint

The solution for Sample 1 is shown in the figure:

The solution for Sample 2 is shown in the figure:

This problem uses bundled testdata and subtask dependencies.

Subtask\rm Subtask nn\leqslant (x,y)(x,y) Special Property Score Subtask Dependencies
11 1010 0x,y200\leqslant x,y\leqslant 20 None 1010 None
22 10310^3 0x,y1030\leqslant x,y\leqslant10^3 1515 11
33 0x,y1090\leqslant x,y\leqslant10^9 1,21,2
44 5×1055\times10^5 109x,y109-10^9\leqslant x,y\leqslant10^9 AA 55 None
55 103x,y103-10^3\leqslant x,y\leqslant10^3 None 2020 1,21,2
66 109x,y109-10^9\leqslant x,y\leqslant10^9 3535 1,2,3,4,51,2,3,4,5

Special property AA: all xix_i are equal.

Constraints: For 100%100\% of the testdata, 1n5×1051\leqslant n\leqslant 5\times 10^5, 0x,y1090\leqslant |x|,|y|\leqslant 10^9, and for any iji\ne j, (xi,yi)(xj,yj)(x_i, y_i)\neq (x_j, y_j).

Translated by ChatGPT 5