CF2055E.Haystacks

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

On the next new moon, the universe will reset, beginning with Florida. It's up to Florida Man to stop it, but he first needs to find an important item.

There are nn haystacks labelled from 11 to nn, where haystack ii contains aia_i haybales. One of the haystacks has a needle hidden beneath it, but you do not know which one. Your task is to move the haybales so that each haystack is emptied at least once, allowing you to check if the needle is hidden under that particular haystack.

However, the process is not that simple. Once a haystack ii is emptied for the first time, it will be assigned a height limit and can no longer contain more than bib_i haybales. More formally, a move is described as follows:

  • Choose two haystacks ii and jj. If haystack ii has not been emptied before, or haystack ii contains strictly less than bib_i haybales, you may move exactly 11 haybale from haystack jj to haystack ii.

Note: Before a haystack is emptied, it has no height limit, and you can move as many haybales as you want onto that haystack.

Compute the minimum number of moves required to ensure that each haystack is emptied at least once, or report that it is impossible.

下一个新月之时,宇宙将重置,而重置将从佛罗里达州开始。阻止这一切的重任落在了“佛罗里达男”身上,但他首先需要找到一件重要物品。

共有 nn 个干草堆,编号从 11 到 nn,其中第 ii 个干草堆包含 aia_i 个干草捆。其中一个干草堆下方藏有一根针,但你并不知道是哪一个。你的任务是移动干草捆,使得每个干草堆至少被清空一次,从而得以检查该干草堆下方是否藏有那根针。

然而,这一过程并不简单。一旦干草堆 ii 首次被清空,它将被赋予一个高度限制,此后其内干草捆数量不得超过 bib_i 个。更准确地说,一次操作定义如下:

  • 选择两个干草堆 ii 和 jj。若干草堆 ii 尚未被清空过,或其当前干草捆数量严格小于 bib_i,则你可以恰好将 11 个干草捆从干草堆 jj 移动到干草堆 ii。

注意:在某个干草堆首次被清空之前,它没有高度限制,因此你可以向其上任意多次移动干草捆(即数量不受限)。

请计算确保每个干草堆至少被清空一次所需的最少操作次数;若不可能实现,则报告为不可能。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤5⋅1052\le n\le 5\cdot 10^5) — the number of haystacks.

The ii-th of the next nn lines contains two integers aia_i and bib_i (1≤ai,bi≤1091\le a_i, b_i\le 10^9) — the initial number of haybales in the ii-th haystack, and the height limit that it is assigned after it is emptied for the first time.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤5⋅1052\le n\le 5\cdot 10^5)—— 表示干草堆的数量。

接下来的 nn 行中,第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤1091\le a_i, b_i\le 10^9)—— 分别表示第 ii 个干草堆的初始干草捆数量,以及该干草堆在首次被清空后所分配的高度限制。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, print a single integer — the minimum number of moves required to ensure that each haystack is emptied at least once. If it is not possible to empty each haystack at least once, output -1.

对于每个测试用例,输出一个整数——确保每个干草堆至少被清空一次所需的最少移动次数。如果无法使每个干草堆至少被清空一次,则输出 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    8
    -1
    8
    9
    14
    15
    19

说明/提示

In the first test case, we can do the following sequence of moves:

  • Move 33 haybales from haystack 11 to haystack 22. Haystack 11 is now emptied, and is assigned a height limit of 55.
  • Move 55 haybales from haystack 22 to haystack 11. Haystack 22 is now emptied, and is assigned a height limit of 44.

The above sequence requires 3+5=83 + 5 = 8 moves. It is not possible to use less than 88 moves as the following sequence of moves is invalid:

  • Move 22 haybales from haystack 22 to haystack 11. Haystack 22 is now emptied, and is assigned a height limit of 44.
  • Move 44 haybales from haystack 11 to haystack 22. Haystack 11 now has 11 haybale, while haystack 22 has 44 haybales.
  • Haystack 11 cannot be emptied as haystack 22 is already at its height limit of 44, so no more haybales can be moved from haystack 11 to haystack 22.

In the second test case, the task is impossible. This is because the height limits of both haystacks are too small that once one of the haystacks is emptied, the other haystack cannot be emptied due to the small height limits.

In the third test case, the following sequence of moves can be shown to be optimal:

  • Move 11 haybale from haystack 11 to haystack 33. Haystack 11 is now emptied, and is assigned a height limit of 33.
  • Move 33 haybales from haystack 22 to haystack 11.
  • Move 11 haybale from haystack 22 to haystack 33. Haystack 22 is now emptied and is assigned a height limit of 33.
  • Move 33 haybales from haystack 33 to haystack 22. Haystack 33 is now emptied, and is assigned a height limit of 11.

The above sequence requires 1+3+1+3=81 + 3 + 1 + 3 = 8 moves.

在第一个测试用例中,我们可以执行以下移动序列:

  • 将 33 个干草捆从干草堆 11 移动到干草堆 22。干草堆 11 此时被清空,并被赋予高度限制 55。
  • 将 55 个干草捆从干草堆 22 移动到干草堆 11。干草堆 22 此时被清空,并被赋予高度限制 44。

上述序列共需 3+5=83 + 5 = 8 次移动。无法使用少于 88 次移动,因为以下移动序列是无效的:

  • 将 22 个干草捆从干草堆 22 移动到干草堆 11。干草堆 22 此时被清空,并被赋予高度限制 44。
  • 将 44 个干草捆从干草堆 11 移动到干草堆 22。此时干草堆 11 剩余 11 个干草捆,而干草堆 22 拥有 44 个干草捆。
  • 干草堆 11 无法被清空,因为干草堆 22 已达到其高度限制 44,因此不能再从干草堆 11 向干草堆 22 移动任何干草捆。

在第二个测试用例中,该任务不可能完成。这是因为两个干草堆的高度限制均过小:一旦其中一个干草堆被清空,另一个干草堆便因高度限制过小而无法被清空。

在第三个测试用例中,可证明以下移动序列为最优解:

  • 将 11 个干草捆从干草堆 11 移动到干草堆 33。干草堆 11 此时被清空,并被赋予高度限制 33。
  • 将 33 个干草捆从干草堆 22 移动到干草堆 11。
  • 将 11 个干草捆从干草堆 22 移动到干草堆 33。干草堆 22 此时被清空,并被赋予高度限制 33。
  • 将 33 个干草捆从干草堆 33 移动到干草堆 22。干草堆 33 此时被清空,并被赋予高度限制 11。

上述序列共需 1+3+1+3=81 + 3 + 1 + 3 = 8 次移动。

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

首页