CodeForces - 1650C - Weight of the System of Nested Segments

On the number line there are mm points, i-th of which has integer coordinate xixi and integer weight wiwi. The coordinates of all points are different, and the points are numbered from 1 to m.

A sequence of nn segments [l1,r1],[l2,r2],…,[ln,rn] is called system of nested segments if for each pair i,ji,j (1≤i<j≤n) the condition 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 nn, find a system of nested segments such that:

  • both ends of each segment are one of mm given points;
  • the sum of the weights 2⋅n of the points used as ends of the segments is minimal.

For example, let m=8m=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
  • weight of the second segment: 10+(−1)=9
  • weight of the third segment: 3+(−2)=1
  • sum of the weights of all the segments in the system: 2+9+1=12

System of three nested segments

Input

The first line of input data contains an integer tt (1≤t≤1e4) —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 nn (1≤n≤1e5) and mm (2⋅n≤m≤2⋅1e5).

The next mm lines contain pairs of integers xixi (−1e9≤xi≤1e9) and wiwi (−1e4≤wi≤1e4) — coordinate and weight of point number ii (1≤i≤m1≤i≤m) respectively. All xixi are different.

It is guaranteed that the sum of mm values over all test cases does not exceed 2⋅1e5

Output

For each test case, output n+1 lines: in the first of them, output the weight of the composed system, and in the next nn lines output exactly two numbers  — the indices of the points which are the endpoints of the i-th segment (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

input

Copy

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

Copy

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 points, so you need to use each of them to compose 3 segments.

思路:要求输出最小值和n组的编号,只要把(m-2*n)个最大值剔除,然后对坐标进行一次排序,输出n组编号即可。

要求记录三个数据:坐标,值,编号,所以开一个结构体数组比较方便。

对结构体数组进行排序的话可以自己写一个cmp函数,然后用快排排序。

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 200010;

typedef struct
{
	int x, w, num;
}Code;

Code a[N];

int cmp(Code aa, Code bb)
{
	return aa.w < bb.w;
}

int cmp1(Code aa, Code bb)
{
	return aa.x < bb.x;
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n, m, ans = 0;
		scanf("%d%d", &n, &m);
		int x, w;
		for(int i = 1; i <= m; i ++)
		{
			scanf("%d%d", &x, &w);
			a[i] = {x, w, i};
			ans += w;
		}
		sort(a + 1, a + m + 1, cmp);
		
		int k = m - n * 2;
		for(int i = 2 * n + 1; i < 2 * n + 1 + k; i ++)
			ans -= a[i].w;
		
		sort(a + 1, a + 1 + 2 * n, cmp1);
		
		printf("%d\n", ans);
		for(int i = 1; i <= n; i ++)
		{
			printf("%d %d\n",a[i].num,a[2 * n + 1 - i].num);
		}
		puts("");
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值