#P9693. [GDCPC2023] New Houses

[GDCPC2023] New Houses

题目描述

With the construction and development of Guangdong, more and more people choose to come to Guangdong to start a new life. In a recently built community, there will be nn people moving into mm houses which are arranged in a row. The houses are numbered from 11 to mm (both inclusive). House uu and vv are neighboring houses, if and only if uv=1|u-v|=1. We need to assign each person to a house so that no two people will move into the same house. If two people move into a pair of neighboring houses, they will become neighbors of each other.

Some people like to have neighbors while some don't. For the ii-th person, if he has at least one neighbor, his happiness will be aia_i; Otherwise if he does not have any neighbor, his happiness will be bib_i.

As the planner of this community, you need to maximize the total happiness.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n5×1051 \le n \le 5 \times 10^5, 1m1091 \le m \le 10^9, nmn \le m) indicating the number of people and the number of houses.

For the following nn lines, the ii-th line contains two integers aia_i and bib_i (1ai,bi1091 \le a_i, b_i \le 10^9) indicating the happiness of the ii-th person with and without neighbors.

It's guaranteed that the sum of nn of all test cases will not exceed 10610^6.

输出格式

For each test case output one line containing one integer indicating the maximum total happiness.

题目大意

【题目描述】

随着广东的建设与发展,越来越多人选择来到广东开始新生活。在一片新建的小区,有 nn 个人要搬进 mm 栋排成一行的房子,房子的编号从 11mm(含两端)。房子 uuvv 相邻,当且仅当 uv=1|u-v|=1。我们需要为每一个人安排一栋房子,要求所有人入住的房子互不相同。若两个人住进了一对相邻的房子,则这两个人互为邻居。

有的人喜欢自己有邻居,而有的人不喜欢。对于第 ii 个人,如果他有至少一位邻居,则他的满意度为 aia_i;否则如果他没有邻居,则他的满意度为 bib_i

您作为小区的规划者,需要最大化所有人的总满意度。

【输入格式】

有多组测试数据。第一行输入一个整数 TT 表示测试数据组数。对于每组测试数据:

第一行输入两个整数 nnmm1n5×1051 \le n \le 5 \times 10^51m1091 \le m \le 10^9nmn \le m),表示人数和房子数。

对于接下来的 nn 行,第 ii 行输入两个整数 aia_ibib_i1ai,bi1091 \le a_i, b_i \le 10^9),表示第 ii 个人在有邻居和没有邻居时的满意度。

保证所有数据 nn 之和不超过 10610^6

【输出格式】

每组数据输出一行一个整数表示最大总满意度。

【样例解释】

对于第一组样例数据,最优方案是让第 11 个人入住房子 11,第 2244 个人入住房子 3355。这样,第 11 个人没有邻居,而第 2244 个人都有邻居。答案为 100+100+100+100=400100 + 100 + 100 + 100 = 400。当然,也可以让第 2244 个人入住房子 1133,第 11 个人入住房子 55,也能得到 400400 的总满意度。

对于第二组样例数据,由于只有 22 栋房子,因此第 11 和第 22 个人必须成为邻居。答案为 1+1=21 + 1 = 2

对于第三组样例数据,最优方案是让第 11 个人入住房子 11,第 22 个人入住房子 33。这样,两个人都没有邻居。答案为 50+1000=105050 + 1000 = 1050

3
4 5
1 100
100 1
100 1
100 1
2 2
1 10
1 10
2 3
100 50
1 1000
400
2
1050

提示

For the first sample test case, the optimal strategy is to let person 11 move into house 11 and let person 22 to 44 move into house 33 to 55. Thus, person 11 have no neighbors while person 22 to 44 have neighbors. The answer is 100+100+100+100=400100 + 100 + 100 + 100 = 400. Of course, we can also let person 22 to 44 move into house 11 to 33 and let person 11 move into house 55. This will also give us 400400 total happiness.

For the second sample test case, as there are only 22 houses, person 11 and 22 have to be neighbors. The answer is 1+1=21 + 1 = 2.

For the third sample test case, the optimal strategy is to let person 11 move into house 11 and let person 22 move into house 33. Thus, both of them have no neighbors. The answer is 50+1000=105050 + 1000 = 1050.