CF1622C.Set or Decrease
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an integer array a1,a2,…,an and integer k.
In one step you can
- either choose some index i and decrease ai by one (make ai=ai−1);
- or choose two indices i and j and set ai equal to aj (make ai=aj).
What is the minimum number of steps you need to make the sum of array i=1∑nai≤k? (You are allowed to make values of array negative).
给你一个整数数组 a1,a2,…,an 和一个整数 k。
每一步你可以执行以下两种操作之一:
- 选择某个下标 i,并将 ai 减少 1(即令 ai=ai−1);
- 选择两个下标 i 和 j,并将 ai 设为等于 aj(即令 ai=aj)。
你需要至少多少步操作,才能使得数组的和 i=1∑nai≤k?(允许数组元素变为负数)
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n and k (1≤n≤2⋅105; 1≤k≤1015) — the size of array a and upper bound on its sum.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the array itself.
It's guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含两个整数 n 和 k(1≤n≤2⋅105;1≤k≤1015)—— 数组 a 的大小及其元素和的上界。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 数组本身。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print one integer — the minimum number of steps to make i=1∑nai≤k.
对于每个测试用例,输出一个整数——使 i=1∑nai≤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 a1 10 times to get the sum lower or equal to k=10.
In the second test case, the sum of array a is already less or equal to 69, so you don't need to change it.
In the third test case, you can, for example:
- set a4=a3=1;
- decrease a4 by one, and get a4=0.
As a result, you'll get array [1,2,1,0,1,2,1] with sum less or equal to 8 in 1+1=2 steps.
In the fourth test case, you can, for example:
- choose a7 and decrease in by one 3 times; you'll get a7=−2;
- choose 4 elements a6, a8, a9 and a10 and them equal to a7=−2.
As a result, you'll get array [1,2,3,1,2,−2,−2,−2,−2,−2] with sum less or equal to 1 in 3+4=7 steps.
在第一个测试用例中,你需要将 a1 减少 10 次,以使数组总和小于或等于 k=10。
在第二个测试用例中,数组 a 的总和已经小于或等于 69,因此无需修改。
在第三个测试用例中,你可以例如:
- 将 a4 和 a3 均设为 1;
- 将 a4 减少 1,得到 a4=0。
最终得到数组 [1,2,1,0,1,2,1],其总和小于或等于 8,共花费 1+1=2 步。
在第四个测试用例中,你可以例如:
- 选择 a7 并将其减少 3 次;得到 a7=−2;
- 选择 4 个元素 a6、a8、a9 和 a10,并将它们均设为 a7=−2。
最终得到数组 [1,2,3,1,2,−2,−2,−2,−2,−2],其总和小于或等于 1,共花费 3+4=7 步。
输入解题思路,AI测评打分。不知道怎么写?