CF2170B.Addition on a Segment

普及-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You start with an integer array aa, which initially consists of nn zeros. You have to perform the following action exactly nn times:

  • choose two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) and assign ai=ai+1a_{i} = a_{i} + 1 for each ii such that l≤i≤rl \le i \le r.

You are given an array bb, consisting of nn integers. Your task is to choose such values ll and rr for each action that:

  • after all nn actions are performed, it's possible to reorder the elements in such a way that aa becomes equal to bb;
  • the maximum value of r−l+1r - l + 1 over all actions is as large as possible.

What's the maximum possible value of r−l+1r - l + 1?

你从一个整数数组 aa 开始,该数组初始时由 nn 个零组成。你需要恰好执行以下操作 nn 次:

  • 选择两个整数 ll 和 rr(满足 1≤l≤r≤n1 \le l \le r \le n),并对每个满足 l≤i≤rl \le i \le r 的下标 ii,令 ai=ai+1a_{i} = a_{i} + 1。

你被给定一个由 nn 个整数组成的数组 bb。你的任务是为每次操作选择合适的 ll 和 rr,使得:

  • 在全部 nn 次操作完成后,可以对数组 aa 的元素进行重排,使其与 bb 完全相等;
  • 所有操作中 r−l+1r - l + 1 的最大值尽可能大。

请问 r−l+1r - l + 1 的最大可能值是多少?

输入格式

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 one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^{5}) — the length of the array bb.

The second line of each test case contains nn integers bib_{i} (0≤bi≤n0 \le b_{i} \le n) — the elements of the array bb.

Additional constraints on the input:

  • there exists at least one way to choose ll and rr for each action and reorder the elements at the end so that aa becomes equal to bb;
  • the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^{5})—— 数组 bb 的长度。

每个测试用例的第二行包含 nn 个整数 bib_{i}(0≤bi≤n0 \le b_{i} \le n)—— 数组 bb 的元素。

输入的额外约束条件:

  • 对于每个操作,均至少存在一种方式选择 ll 和 rr,并在最后重新排列元素,使得 aa 变为 bb;
  • 所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output one integer — the answer to the problem.

对于每个测试用例,输出一个整数——该问题的答案。

输入输出样例

  • 输入#1

    3
    5
    0 5 1 0 1
    3
    3 2 1
    5
    1 1 1 1 1

    输出#1

    3
    3
    1

说明/提示

Consider the first test case. If the nn actions were as follows:

  • l=3l = 3 and r=3r = 3
  • l=1l = 1 and r=3r = 3
  • l=3l = 3 and r=3r = 3
  • l=3l = 3 and r=3r = 3
  • l=3l = 3 and r=3r = 3

The array a=[1,1,5,0,0]a = [1, 1, 5, 0, 0], so you can reorder the elements to make it equal to [0,5,1,0,1][0, 5, 1, 0, 1]. As can be seen in this case, the maximum value of r−l+1r - l + 1 is 33. It can be shown that this is the optimal answer.

In the second test case:

  • l=1l = 1 and r=3r = 3
  • l=2l = 2 and r=3r = 3
  • l=3l = 3 and r=3r = 3

The answer is 33.

考虑第一个测试用例。若 nn 个操作如下所示:

  • l=3l = 3 且 r=3r = 3
  • l=1l = 1 且 r=3r = 3
  • l=3l = 3 且 r=3r = 3
  • l=3l = 3 且 r=3r = 3
  • l=3l = 3 且 r=3r = 3

则数组 a=[1,1,5,0,0]a = [1, 1, 5, 0, 0],因此你可以重新排列其元素,使其变为 [0,5,1,0,1][0, 5, 1, 0, 1]。如本例所示,r−l+1r - l + 1 的最大值为 33。可以证明这是最优答案。

在第二个测试用例中:

  • l=1l = 1 且 r=3r = 3
  • l=2l = 2 且 r=3r = 3
  • l=3l = 3 且 r=3r = 3

答案为 33。

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

首页