Codeforces Round #689 (Div. 2) D. Divide and Summarize(二分 枚举)

题目链接:https://codeforc.es/contest/1461/problem/D

Mike received an array a of length n as a birthday present and decided to test how pretty it is.

An array would pass the i-th prettiness test if there is a way to get an array with a sum of elements totaling si, using some number (possibly zero) of slicing operations.

An array slicing operation is conducted in the following way:

assume mid=⌊max(array)+min(array)2⌋, where max and min — are functions that find the maximum and the minimum array elements. In other words, mid is the sum of the maximum and the minimum element of array divided by 2 rounded down.
Then the array is split into two parts left and right. The left array contains all elements which are less than or equal mid, and the right array contains all elements which are greater than mid. Elements in left and right keep their relative order from array.
During the third step we choose which of the left and right arrays we want to keep. The chosen array replaces the current one and the other is permanently discarded.
You need to help Mike find out the results of q prettiness tests.

Note that you test the prettiness of the array a, so you start each prettiness test with the primordial (initial) array a. Thus, the first slice (if required) is always performed on the array a.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤100).

The first line of each test case contains two integers n and q (1≤n,q≤105) — the length of the array a and the total number of prettiness tests.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤106) — the contents of the array a.

Next q lines of each test case contain a single integer si (1≤si≤109) — the sum of elements which Mike wants to get in the i-th test.

It is guaranteed that the sum of n and the sum of q does not exceed 105 (∑n,∑q≤105).

Output
Print q lines, each containing either a “Yes” if the corresponding prettiness test is passed and “No” in the opposite case.

Example

input

2
5 5
1 2 3 4 5
1
8
9
12
6
5 5
3 1 3 1 3
1
2
3
9
11

output

Yes
No
Yes
No
Yes
No
Yes
No
Yes
Yes

分析

我们只要按照题目要求,将可能的情况二分枚举出来,最后判断有没有可能就行了。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll a[100007];
ll sum[100007];
map<ll,ll> ans;

void Do(int l,int r)
{
	int mid = (a[l] + a[r]) / 2;
	ans[sum[r] - sum[l - 1]] = 1;
	int pos = upper_bound(a + l, a + r + 1, mid) - a;
	if(pos <= r)
	{
		Do(l, pos - 1);
		Do(pos, r);
	}
	return;
}

void solve()
{
	ans.clear();
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	sort(a + 1, a + 1 + n);
	for(int i=1;i<=n;i++) sum[i] = sum[i - 1] + a[i];
	Do(1, n);
	ll x;
	while(q--)
	{
		scanf("%lld",&x);
		if(ans[x] == 1) printf("Yes\n");
		else printf("No\n");
	}
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		solve();
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值