C. Weight of the System of Nested Segments
On the number line there are m points, i i i-th of which has integer coordinate x i x_i xi and integer weight w i w_i wi. The coordinates of all points are different, and the points are numbered from 1 1 1 to m m m.
A sequence of n segments [ l 1 , r 1 ] , [ l 2 , r 2 ] , … , [ l n , r n ] [l1,r1],[l2,r2],…,[ln,rn] [l1,r1],[l2,r2],…,[ln,rn] is called system of nested segments if for each pair i , j ( 1 ≤ i < j ≤ n ) i,j (1≤i<j≤n) i,j(1≤i<j≤n) the condition l i < l j < r j < r i l_i<l_j<r_j<r_i li<lj<rj<ri is satisfied. In other words, the second segment is strictly inside the first one, the third segment is strictly inside the second one, and so on.
For a given number n n n, find a system of nested segments such that:
- both ends of each segment are one of m m m given points;
- the sum of the weights 2 ⋅ n 2⋅n 2⋅n of the points used as ends of the segments is minimal.
For example, let m = 8 m=8 m=8. The given points are marked in the picture, their weights are marked in red, their coordinates are marked in blue. Make a system of three nested segments:
- weight of the first segment: 1 + 1 = 2 1+1=2 1+1=2
- weight of the second segment: 10 + ( − 1 ) = 9 10+(−1)=9 10+(−1)=9
- weight of the third segment: 3 + ( − 2 ) = 1 3+(−2)=1 3+(−2)=1
- sum of the weights of all the segments in the system: 2 + 9 + 1 = 12 2+9+1=12 2+9+1=12
System of three nested segments Input
The first line of input data contains an integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) —the number of input test cases.
An empty line is written before each test case.
The first line of each test case contains two positive integers n ( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10^5) n(1≤n≤105) and m ( 2 ⋅ n ≤ m ≤ 2 ⋅ 1 0 5 ) m (2⋅n≤m≤2⋅10^5) m(2⋅n≤m≤2⋅105).
The next m m m lines contain pairs of integers x i ( − 109 ≤ x i ≤ 1 0 9 ) x_i (−109≤x_i≤10^9) xi(−109≤xi≤109) and w i ( − 1 0 4 ≤ w i ≤ 1 0 4 ) w_i (−10^4≤w_i≤10^4) wi(−104≤wi≤104) — coordinate and weight of point number i ( 1 ≤ i ≤ m ) i (1≤i≤m) i(1≤i≤m) respectively. All xi are different.
It is guaranteed that the sum of m m m values over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
Output
For each test case, output n + 1 n+1 n+1 lines: in the first of them, output the weight of the composed system, and in the next n n n lines output exactly two numbers — the indices of the points which are the endpoints of the i i i-th segment ( 1 ≤ i ≤ n ) (1≤i≤n) (1≤i≤n). The order in which you output the endpoints of a segment is not important — you can output the index of the left endpoint first and then the number of the right endpoint, or the other way around.
If there are several ways to make a system of nested segments with minimal weight, output any of them.
Example
input3 3 8 0 10 -2 1 4 10 11 20 7 -1 9 1 2 3 5 -2 3 6 -1 2 1 3 3 -1 2 4 4 0 8 2 2 5 5 -1 3 -2 1 0 -2 0 -5 -3
output
12 2 6 5 1 7 8 10 1 6 5 2 3 4 -6 5 1 4 2
Note
The first test case coincides with the example from the condition. It can be shown that the weight of the composed system is minimal.
The second test case has only 6 6 6 points, so you need to use each of them to compose 3 3 3 segments.
题目大意:
找出 n 条边使得每条边两端点的权值加起来的和最小。(下一条边一定在上一条边内)
解题思路:
将所有顶点记录,接着按照权重大小从小到打将顶点排序,然后还是排序,不过是按照顶点的坐标排序,特别的是需要排的个数不一定是全部顶点,而是边的两倍(一边两顶点)。然后就是从两边向中间打印两顶点输入时的出现顺序(从1开始)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
struct node
{
int idx, x, w; //出现顺序、坐标、权值
}p[N];
bool cmpX(node a, node b)
{
return a.x < b.x;
}
bool cmpW(node a, node b)
{
return a.w < b.w;
}
int main()
{
int t; cin>>t;
while(t--)
{
int n, m; cin>>n>>m;
for(int i=0; i<m; i++)
{
p[i].idx = i+1;
cin>>p[i].x>>p[i].w;
}
sort(p, p+m, cmpW);
sort(p, p+2*n, cmpX);
int sum = 0; //计算两次排序后前 2n 个顶点的权值和
for(int i=0; i<2*n; i++) sum += p[i].w;
cout<<sum<<'\n';
int l = 0, r = 2*n-1;
while(n--)
{
//打印两端顶点
cout<<p[l++].idx<<' '<<p[r--].idx<<'\n';
}
cout<<'\n';
}
return 0;
}