#P9559. [SDCPC2023] Fast and Fat

[SDCPC2023] Fast and Fat

题目描述

You're participating in a team competition of trail running. There are nn members in your team where viv_i is the speed of the ii-th member and wiw_i is his/her weight.

The competition allows each team member to move alone or carry another team member on their back. When member ii carries member jj, if member ii's weight is greater than or equal to member jj's weight, member ii's speed remains unchanged at viv_i. However, if member ii's weight is less than member jj's weight, member ii's speed will decrease by the difference of their weight and becomes vi(wjwi)v_i - (w_j - w_i). If member ii's speed will become negative, then member ii is not able to carry member jj. Each member can only carry at most one other member. If a member is being carried, he/she cannot carry another member at the same time.

For all members not being carried, the speed of the slowest member is the speed of the whole team. Find the maximum possible speed of the whole team.

输入格式

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 an integer nn (1n1051 \le n \le 10^5) indicating the number of team members.

For the following nn lines, the ii-th line contains two integers viv_i and wiw_i (1vi,wi1091 \le v_i, w_i \le 10^9) indicating the speed and weight of the ii-th member.

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

输出格式

For each test case output one line containing one integer indicating the maximum speed of the whole team.

题目大意

【题目描述】

您正在参加一场团体越野比赛。您的队伍共有 nn 名队员,其中第 ii 名队员的速度为 viv_i,体重为 wiw_i

比赛允许每名队员独立行动,也允许一名队员背着另一名队员一起行动。当队员 ii 背着队员 jj 时,如果队员 ii 的体重大于等于队员 jj,则队员 ii 的移动速度不会变化,仍然为 viv_i;如果队员 ii 的体重小于队员 jj,则队员 ii 的移动速度会减去两者的体重差值,即变为 vi(wjwi)v_i - (w_j - w_i)。如果队员 ii 的移动速度将变为负数,则队员 ii 无法背起队员 jj。每名队员最多只能背负另一名队员,被背负的队员无法同时背负其他队员。

所有未被背负的队员中,最慢的队员的速度,即为整个队伍的速度。求整个队伍能达到的最大速度。

【输入格式】

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

第一行输入一个整数 nn1n1051 \le n \le 10^5)表示队员人数。

对于接下来 nn 行,第 ii 行输入两个整数 viv_iwiw_i1vi,wi1091 \le v_i, w_i \le 10^9)表示第 ii 名队员的速度和体重。

保证所有数据中 nn 之和不超过 10510^5

【输出格式】

每组数据输出一个整数,表示整个队伍可以达到的最大速度。

【样例解释】

样例数据的最优策略如下:

  • 队员 11 背起队员 44。因为 w1>w4w_1 > w_4,因此队员 11 速度不变,仍然为 1010
  • 队员 33 背起队员 22。因为 w3<w2w_3 < w_2,因此队员 33 的速度减少 w2w3=2w_2 - w_3 = 2,即速度变为 102=810 - 2 = 8
  • 队员 55 独立行动,速度为 99

因此答案为 88

2
5
10 5
1 102
10 100
7 4
9 50
2
1 100
10 1
8
1

提示

The optimal strategy for the sample test case is shown as follows:

  • Let member 11 carry member 44. As w1>w4w_1 > w_4, member 11's speed remains unchanged, which is still 1010.
  • Let member 33 carry member 22. As w3<w2w_3 < w_2, member 33's speed will decrease by w2w3=2w_2 - w_3 = 2 and becomes 102=810 - 2 = 8.
  • Member 55 shall move alone. His/Her speed is 99.

So the answer is 88.