CF1692E.Binary Deque
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Slavic has an array of length n consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.
What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to s after performing all the operations? In case the sum s can't be obtained after any amount of operations, you should output -1.
斯拉维克有一个长度为 n 的数组,其中仅包含 0 和 1。在一次操作中,他可以移除数组的第一个或最后一个元素。
在执行完所有操作后,若要使数组的总和恰好等于 s,斯拉维克至少需要执行多少次操作?如果无论执行多少次操作都无法使数组总和等于 s,则应输出 -1。
输入格式
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 s (1≤n,s≤2⋅105) — the length of the array and the needed sum of elements.
The second line of each test case contains n integers ai (0≤ai≤1) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含两个整数 n 和 s(1≤n,s≤2⋅105)—— 数组的长度以及所需元素之和。
每个测试用例的第二行包含 n 个整数 ai(0≤ai≤1)—— 数组的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to s, or -1 if obtaining an array with sum s isn't possible.
对于每个测试用例,输出一个整数——使数组总和等于 s 所需的最少操作次数;如果无法得到总和为 s 的数组,则输出 −1。
输入输出样例
输入#1
7 3 1 1 0 0 3 1 1 1 0 9 3 0 1 0 1 1 1 0 0 1 6 4 1 1 1 1 1 1 5 1 0 0 1 1 0 16 2 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 6 3 1 0 1 0 0 0
输出#1
0 1 3 2 2 7 -1
说明/提示
In the first test case, the sum of the whole array is 1 from the beginning, so we don't have to make any operations.
In the second test case, the sum of the array is 2 and we want it to be equal to 1, so we should remove the first element. The array turns into [1,0], which has a sum equal to 1.
In the third test case, the sum of the array is 5 and we need it to be 3. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into [0,1,1,1,0,0], which has a sum equal to 3.
在第一个测试用例中,整个数组的和初始即为 1,因此我们无需进行任何操作。
在第二个测试用例中,数组的和为 2,而我们希望其等于 1,因此应删除第一个元素。数组变为 [1,0],其和等于 1。
在第三个测试用例中,数组的和为 5,而我们需要其等于 3。我们可以通过删除前两个元素及最后一个元素来得到该和,总共执行三次操作。数组变为 [0,1,1,1,0,0],其和等于 3。
输入解题思路,AI测评打分。不知道怎么写?