CF1622C.Set or Decrease

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an integer array a1,a2,…,ana_1, a_2, \dots, a_n and integer kk.

In one step you can

  • either choose some index ii and decrease aia_i by one (make ai=ai−1a_i = a_i - 1);
  • or choose two indices ii and jj and set aia_i equal to aja_j (make ai=aja_i = a_j).

What is the minimum number of steps you need to make the sum of array ∑i=1nai≤k\sum\limits_{i=1}^{n}{a_i} \le k? (You are allowed to make values of array negative).

给你一个整数数组 a1,a2,…,ana_1, a_2, \dots, a_n 和一个整数 kk。

每一步你可以执行以下两种操作之一:

  • 选择某个下标 ii,并将 aia_i 减少 1(即令 ai=ai−1a_i = a_i - 1);
  • 选择两个下标 ii 和 jj,并将 aia_i 设为等于 aja_j(即令 ai=aja_i = a_j)。

你需要至少多少步操作,才能使得数组的和 ∑i=1nai≤k\sum\limits_{i=1}^{n}{a_i} \le k?(允许数组元素变为负数)

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 1≤k≤10151 \le k \le 10^{15}) — the size of array aa and upper bound on its sum.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the array itself.

It's guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;1≤k≤10151 \le k \le 10^{15})—— 数组 aa 的大小及其元素和的上界。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组本身。

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

输出格式

For each test case, print one integer — the minimum number of steps to make ∑i=1nai≤k\sum\limits_{i=1}^{n}{a_i} \le k.

对于每个测试用例,输出一个整数——使 ∑i=1nai≤k\sum\limits_{i=1}^{n}{a_i} \le k 所需的最少步数。

输入输出样例

  • 输入#1

    4
    1 10
    20
    2 69
    6 9
    7 8
    1 2 1 3 1 2 1
    10 1
    1 2 3 1 2 6 1 6 8 10

    输出#1

    10
    0
    2
    7

说明/提示

In the first test case, you should decrease a1a_1 1010 times to get the sum lower or equal to k=10k = 10.

In the second test case, the sum of array aa is already less or equal to 6969, so you don't need to change it.

In the third test case, you can, for example:

  1. set a4=a3=1a_4 = a_3 = 1;
  2. decrease a4a_4 by one, and get a4=0a_4 = 0.

As a result, you'll get array [1,2,1,0,1,2,1][1, 2, 1, 0, 1, 2, 1] with sum less or equal to 88 in 1+1=21 + 1 = 2 steps.

In the fourth test case, you can, for example:

  1. choose a7a_7 and decrease in by one 33 times; you'll get a7=−2a_7 = -2;
  2. choose 44 elements a6a_6, a8a_8, a9a_9 and a10a_{10} and them equal to a7=−2a_7 = -2.

As a result, you'll get array [1,2,3,1,2,−2,−2,−2,−2,−2][1, 2, 3, 1, 2, -2, -2, -2, -2, -2] with sum less or equal to 11 in 3+4=73 + 4 = 7 steps.

在第一个测试用例中,你需要将 a1a_1 减少 10 次,以使数组总和小于或等于 k=10k = 10。

在第二个测试用例中,数组 aa 的总和已经小于或等于 6969,因此无需修改。

在第三个测试用例中,你可以例如:

  1. 将 a4a_4 和 a3a_3 均设为 11;
  2. 将 a4a_4 减少 1,得到 a4=0a_4 = 0。

最终得到数组 [1,2,1,0,1,2,1][1, 2, 1, 0, 1, 2, 1],其总和小于或等于 88,共花费 1+1=21 + 1 = 2 步。

在第四个测试用例中,你可以例如:

  1. 选择 a7a_7 并将其减少 3 次;得到 a7=−2a_7 = -2;
  2. 选择 44 个元素 a6a_6、a8a_8、a9a_9 和 a10a_{10},并将它们均设为 a7=−2a_7 = -2。

最终得到数组 [1,2,3,1,2,−2,−2,−2,−2,−2][1, 2, 3, 1, 2, -2, -2, -2, -2, -2],其总和小于或等于 11,共花费 3+4=73 + 4 = 7 步。

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

首页