CF1650C.Weight of the System of Nested Segments

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

On the number line there are mm points, ii-th of which has integer coordinate xix_i and integer weight wiw_i. 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][l_1, r_1], [l_2, r_2], \dots, [l_n, r_n] is called system of nested segments if for each pair i,ji, j (1≤i<j≤n1 \le i \lt j \le n) the condition li<lj<rj<ril_i \lt l_j \lt r_j \lt r_i 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\cdot 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

System of three nested segments

数轴上有 mm 个点,其中第 ii 个点具有整数坐标 xix_i 和整数权重 wiw_i。所有点的坐标互不相同,且这些点编号为 11 至 mm。

一个由 nn 个区间 [l1,r1],[l2,r2],…,[ln,rn][l_1, r_1], [l_2, r_2], \dots, [l_n, r_n] 构成的序列被称为嵌套区间系统,当且仅当对任意一对下标 i,ji, j(满足 1≤i<j≤n1 \le i \lt j \le n),均有 li<lj<rj<ril_i \lt l_j \lt r_j \lt r_i 成立。换言之,第二个区间严格包含于第一个区间内部,第三个区间严格包含于第二个区间内部,依此类推。

给定正整数 nn,请找出一个嵌套区间系统,使得:

  • 每个区间的两个端点均来自给定的 mm 个点;
  • 所有 2n2n 个被选作区间端点的点的权重之和最小。

例如,设 m=8m = 8。图中用标记标出了给定的点,其权重以红色标出,坐标以蓝色标出。构造一个由三个嵌套区间组成的系统:

  • 第一个区间的权重:1+1=21 + 1 = 2
  • 第二个区间的权重:10+(−1)=910 + (-1) = 9
  • 第三个区间的权重:3+(−2)=13 + (-2) = 1
  • 该嵌套区间系统中所有区间的权重总和:2+9+1=122 + 9 + 1 = 12

三个嵌套区间的系统

输入格式

The first line of input data contains an integer tt (1≤t≤1041 \le t \le 10^4) —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 \le n \le 10^5) and mm (2⋅n≤m≤2⋅1052 \cdot n \le m \le 2 \cdot 10^5).

The next mm lines contain pairs of integers xix_i (−109≤xi≤109-10^9 \le x_i \le 10^9) and wiw_i (−104≤wi≤104-10^4 \le w_i \le 10^4) — coordinate and weight of point number ii (1≤i≤m1 \le i \le m) respectively. All xix_i are different.

It is guaranteed that the sum of mm values over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示输入测试用例的数量。

每个测试用例之前均有一空行。

每个测试用例的第一行包含两个正整数 nn(1≤n≤1051 \le n \le 10^5)和 mm(2⋅n≤m≤2⋅1052 \cdot n \le m \le 2 \cdot 10^5)。

接下来的 mm 行每行包含一对整数 xix_i(−109≤xi≤109-10^9 \le x_i \le 10^9)和 wiw_i(−104≤wi≤104-10^4 \le w_i \le 10^4),分别表示第 ii 个点(1≤i≤m1 \le i \le m)的坐标与权重。所有 xix_i 互不相同。

保证所有测试用例中 mm 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

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 \le i \le 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.

对于每个测试用例,输出 n+1n + 1 行:第一行输出组合系统的总重量;接下来的 nn 行中,每行恰好输出两个数——即第 ii 个线段(1≤i≤n1 \le i \le n)的两个端点的索引。输出线段端点的顺序无关紧要——你可以先输出左端点的索引、再输出右端点的索引,也可以反过来。

如果存在多种方式构造具有最小总重量的嵌套线段系统,则输出其中任意一种即可。

输入输出样例

  • 输入#1

    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

    输出#1

    12
    2 6
    5 1
    7 8
    
    10
    1 6
    5 2
    3 4
    
    -6
    5 1
    4 2

说明/提示

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.

第一个测试用例与题面中的示例一致。可以证明,所构成系统的总权重最小。

第二个测试用例仅有 66 个点,因此你需要使用全部这 66 个点来构成 33 条线段。

输入解题思路,AI测评打分。不知道怎么写?

首页