Babushka and her pierogi

시간 제한6초메모리 제한1024 MB

요약
각 접시의 현재 값과 목표 값이 주어질 때, 값 x와 y를 맞바꾸는 비용이 |x-y|+C일 때 모든 접시를 목표 값으로 만드는 최소 비용 교환 순서를 찾는다.
난이도

어려움10점 중 8점

유형
그리디, 그래프, 정렬, 구현
정답자
아직 제출이 없습니다

문제

Babushka Bajtmiła is throwing a party! And the main dish served will be her famous pierogi.

There will be nn plates available during the party, and Babushka plans to put exactly p_ip\_i pierogi on the ii-th plate (all the values p_ip\_i are distinct). Though the task seemed too heavy for an old lady, Bajtmiła has stood up to the challenge and almost instantly prepared all of the pierogi needed, divided among nn plates with (p_1,…,p_n)(p\_1, \ldots, p\_n) pierogi. Next, Bajtmiła has distributed the plates among the plates. However, she soon realized that while she got the numbers right, she messed up the order of the plates.

Bajtmiła is quite tired and is only willing to perform one type of operation: she can choose two plates numbered ii and jj, and swap the amounts of pierogi on each plate. In other words, if there are xx pierogi at the plate ii, and yy pierogi at the plate jj, then after this operation there will be yy pierogi at the plate ii, and xx pierogi at plate jj. Such an operation takes exactly ∣x−y∣+C|x - y| + C seconds to perform -- CC seconds for finding a proper spoon, and 11 second for each of pierogi moved.

The party is about to start very soon! Now, Bajtmiła won't allow you to touch anything in the kitchen, but she has put her trust in your algorithmic skills. She asked you to find a sequence of operations restoring the desired order of numbers, and must do it in the shortest time possible. Can you help Bajtmiła?

입력

The first line of input contains the number of test cases zz (1≤z≤10001 \leq z \leq 1000). The descriptions of the test cases follow.

The first line of each test case consists of two numbers nn and CC (1≤n≤200,000,1≤C≤1091 \le n \le 200\\,000, 1 \le C \le 10^9) with their meaning described in the statement above.

Next nn lines describe consecutive plates. The ii-th line contains two numbers a_ia\_i and p_ip\_i (1≤a_i,p_i≤1091 \le a\_i, p\_i \le 10^9) indicating the current and the desired amount of pierogi on ii-th plate respectively.

In each test case, numbers a_ia\_i are distinct. Furthermore, the sets a_1,a_2,...,a_n\\{a\_1, a\_2, ... , a\_n\\} and p_1,p_2,...,p_n\\{p\_1, p\_2, ... , p\_n\\} are the same.

The sum over values nn in all test cases does not exceed 1,000,0001\\,000\\,000.

출력

For each test case, your output must match the following description:

In the first line, print two integers SS and KK -- the total time and the number of operations in your solution respectively.

Next KK lines of your output should describe your solution. In the kk-th line print two numbers x_kx\_k and y_ky\_k, indicating that the kk-th operation in your solution swaps the amounts of pierogi at x_kx\_k-th and y_ky\_k-th plate.

After all operations from your solution, the ii-th plate must contain exactly p_ip\_i pierogi.

힌트

A sequence (2,3,1,4)(2,3,1,4) should become the sequence (4,2,1,3)(4,2,1,3). We first perform the operation on the first two plates, obtaining the sequence (3,2,1,4)(3,2,1,4) at the cost ∣3−2∣+2=3|3-2|+2 = 3. The second and final operation swaps the pierogis from the first and fourth plate, achieving the desired sequence (4,2,1,3)(4,2,1,3). The cost of this operation is ∣3−4∣+2=3|3-4|+2 = 3, and the total cost is 66, which is the minimal possible.

예제1

  1. 예제 1

    입력
    1
    4 2
    2 4
    3 2
    1 1
    4 3
    
    예상 출력
    6 2
    2 1
    4 1