1692E. Binary Deque

原题链接

E. Binary Deque

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Slavic has an array of length nn 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 ss after performing all the operations? In case the sum ss can't be obtained after any amount of operations, you should output -1.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains two integers nn and ss (1≤n,s≤2⋅1051≤n,s≤2⋅105) — the length of the array and the needed sum of elements.

The second line of each test case contains nn integers aiai (0≤ai≤10≤ai≤1) — the elements of the array.

It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to ss, or -1 if obtaining an array with sum ss isn't possible.

Example

input

Copy

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

output

Copy

0
1
3
2
2
7
-1

Note

In the first test case, the sum of the whole array is 11 from the beginning, so we don't have to make any operations.

In the second test case, the sum of the array is 22 and we want it to be equal to 11, so we should remove the first element. The array turns into [1,0][1,0], which has a sum equal to 11.

In the third test case, the sum of the array is 55 and we need it to be 33. 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][0,1,1,1,0,0], which has a sum equal to 33.

大意就是说可以删除数组第一个和最后一个,该操作可以重复无限次,找到最小的操作次数从而让剩下的数组元素加起来等于输入的s。

思路是用双指针从头到尾扫一遍如果数组加起来大于s那就剔除掉第一个。详细看代码。

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
using namespace std;
const int manx = 2e5 + 10;
int a[manx];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		long long n, s;
		cin>> n >> s;
		long long sum = 0;
		int cnt = 0;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			sum += a[i];
		}
		if (sum < s)//不能做到就输出-1。
		{
			cout << -1 << endl;
			
		}
		else
		{
			int ans = 0;
			for (int i = 1, j = 1; j <= n; j++)//j在i前
			{
				cnt += a[j];
				while (i<j && cnt>s)//满足条件就把当前数组的第一位剔除。
				{
					cnt -= a[i++];
				}
				if (cnt == s)
				{
					ans = max(ans, j - i + 1);//取j和i距离最长
				}
			}
			cout << n-ans << endl;//用n减去就是操作数。
		}
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值