cf #776 div.2 C. Weight of the System of Nested Segments

题目传送门

题目描述

C. Weight of the System of Nested Segments

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

A sequence of nn segments [l1,r1],[l2,r2],…,[ln,rn][l1,r1],[l2,r2],…,[ln,rn] is called system of nested segments if for each pair i,ji,j (1≤i<j≤n1≤i<j≤n) the condition li<lj<rj<rili<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⋅n2⋅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=21+1=2
  • weight of the second segment: 10+(−1)=910+(−1)=9
  • weight of the third segment: 3+(−2)=13+(−2)=1
  • sum of the weights of all the segments in the system: 2+9+1=122+9+1=12

imgSystem of three nested segments

Input

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

The next mm lines contain pairs of integers xixi (−109≤xi≤109−109≤xi≤109) and wiwi (−104≤wi≤104−104≤wi≤104) — 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⋅1052⋅105.

Output

For each test case, output n+1n+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 ii-th segment (1≤i≤n1≤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 66 points, so you need to use each of them to compose 33 segments.

阅读理解

分析

首先读入每个点的:坐标、权重、id(这是第几个点)

然后按照权重排序,取前2*n个,remove其他的

然后把剩下的点按照坐标排序

求这2n个线段权重的和

然后输出每个segment线段两端的id即可

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
//typedef pair<int, int> PII;

struct POINT
{
	int pos, w, id;	
};

int t;

bool cmpw(POINT a, POINT b)
{
	return a.w < b.w;
} 

bool cmpp(POINT a, POINT b)
{
	return a.pos < b.pos;
}

void solve()
{
	int n, m;
	vector<POINT> h;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		POINT a;
		cin >> a.pos >> a.w;
		a.id = i+1;
		h.push_back(a);
	}

	
	sort(h.begin(), h.end(), cmpw); // 按照权重排序 
	
	// 只保留前2*n个 
	while(h.size() > 2*n)
	{
		h.pop_back();
	}
	
	sort(h.begin(), h.end(), cmpp); // 按坐标排序
	
	int sum = 0;
	for(int i = 0; i < 2*n; i++) sum += h[i].w;
	
	cout << sum << endl;
	for(int i = 0; i < n; i++) cout << h[i].id << " " << h[2*n - i - 1].id << endl;
	cout << endl; 
}

int main()
{
	cin >> t;
	while(t--)
	{
		solve();
	}
	return 0;
}

时间复杂度

参考文章

https://codeforces.com/blog/entry/100712

posted @ 2022-03-15 20:01  VanHope  阅读(69)  评论(0编辑  收藏  举报