CF1692E.Binary Deque
普及/提高-
通过率:0%
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.
输入格式
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 .
输出格式
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.
输入输出样例
输入#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 .