CF 1650C - Weight of the System of Nested Segments

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(1i<jn) 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 2n 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

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(1t104) —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(1n105) and m ( 2 ⋅ n ≤ m ≤ 2 ⋅ 1 0 5 ) m (2⋅n≤m≤2⋅10^5) m(2nm2105).
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(109xi109) and w i ( − 1 0 4 ≤ w i ≤ 1 0 4 ) w_i (−10^4≤w_i≤10^4) wi(104wi104) — coordinate and weight of point number i ( 1 ≤ i ≤ m ) i (1≤i≤m) i(1im) 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 2105.
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) (1in). 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
input

3

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生ono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值