【Codeforces 1461 D】Divide and Summarize,二分,分治,贪心

problem

D. Divide and Summarize
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
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
outputCopy
Yes
No
Yes
No
Yes
No
Yes
No
Yes
Yes
Note
Explanation of the first test case:

We can get an array with the sum s1=1 in the following way:
1.1 a=[1,2,3,4,5], mid=1+52=3, left=[1,2,3], right=[4,5]. We choose to keep the left array.

1.2 a=[1,2,3], mid=1+32=2, left=[1,2], right=[3]. We choose to keep the left array.

1.3 a=[1,2], mid=1+22=1, left=[1], right=[2]. We choose to keep the left array with the sum equalling 1.

It can be demonstrated that an array with the sum s2=8 is impossible to generate.
An array with the sum s3=9 can be generated in the following way:
3.1 a=[1,2,3,4,5], mid=1+52=3, left=[1,2,3], right=[4,5]. We choose to keep the right array with the sum equalling 9.

It can be demonstrated that an array with the sum s4=12 is impossible to generate.
We can get an array with the sum s5=6 in the following way:
5.1 a=[1,2,3,4,5], mid=1+52=3, left=[1,2,3], right=[4,5]. We choose to keep the left with the sum equalling 6.

Explanation of the second test case:

It can be demonstrated that an array with the sum s1=1 is imposssible to generate.
We can get an array with the sum s2=2 in the following way:
2.1 a=[3,1,3,1,3], mid=1+32=2, left=[1,1], right=[3,3,3]. We choose to keep the left array with the sum equalling 2.

It can be demonstrated that an array with the sum s3=3 is imposssible to generate.
We can get an array with the sum s4=9 in the following way:
4.1 a=[3,1,3,1,3], mid=1+32=2, left=[1,1], right=[3,3,3]. We choose to keep the right array with the sum equalling 9.

We can get an array with the sum s5=11 with zero slicing operations, because array sum is equal to 11.

D.划分和总结
每个测试的时限2秒
每个测试的内存限制256 MB
输入标准输入
输出标准输出
Mike收到了长度为n的数组a作为生日礼物,并决定测试它的长短。

如果有一种方法可以通过使用一定数量(可能为零)的切片操作来获得一个元素总数为si的数组,则该数组将通过第i个测试。

数组切片操作按以下方式进行:

假设mid =⌊max(array)+ min(array)2⌋,其中max和min是查找最大和最小数组元素的函数。换句话说,mid是数组的最大和最小元素的总和除以2的整数。
然后将数组分为左右两部分。左边的数组包含小于或等于mid的所有元素,右边的数组包含大于mid的所有元素。左侧和右侧的元素保持其相对顺序与数组无关。
在第三步中,我们选择要保留的左右数组。选定的阵列将替换当前的阵列,而另一阵列将被永久丢弃。
您需要帮助Mike找出q个美化测试的结果。

请注意,您测试了数组a的美观性,因此您从原始(初始)数组a开始了每个美观性测试。因此,第一切片(如果需要)总是在数组a上执行。

输入
每个测试包含一个或多个测试用例。第一行包含测试用例的数量t(1≤t≤100)。

每个测试用例的第一行包含两个整数n和q(1≤n,q≤105)-数组a的长度和漂亮测试的总数。

每个测试用例的第二行包含n个整数a1,a2,…,an(1≤ai≤106)—数组a的内容。

每个测试用例的接下来的q行包含单个整数si(1≤si≤109)-Mike希望在第i个测试中获得的元素之和。

保证n和q之和不超过105(∑n,∑q≤105)。

输出
打印q行,如果通过了相应的测试,则每行包含“是”,反之则包含“否”。

例子
inputCopy
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
outputCopy
是的

是的

是的

是的

是的
是的

solution

/*
题意:
+ 给出一个长为n的数组,q个询问能否通过操作后某段区间的和获得该数
+ 定义操作,每次将数组分成两部分(左边<=mid),mid=(max+min)/2
思路:
+ 对于给定数组,所有可能操作的值都是确定的O(nlogn)
+ 维护集合然后统计即可。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
int a[maxn];
set<LL>se;
void dfs(int l, int r){
	if(l>r){
		return ;
	}
	
	LL sum = 0;//wa7
	for(int i = l; i <= r; i++)sum += a[i];
	se.insert(sum);
	//cout<<l<<" "<<r<<" "<<mid<<"\n";
	
	//sort(a+l,a+r+1);
	int mid = upper_bound(a+l,a+r+1,(a[l]+a[r])/2)-a;
	//cout<<l<<" "<<r<<" "<<mid<<" "<<(a[l]+a[r])/2<<"\n";
	if(mid==r+1){
		return ;
	}
	
	if(mid-1>=l)dfs(l,mid-1);
	if(mid<=r)dfs(mid,r);
}
int main(){
	int T;  cin>>T;
	while(T--){
		int n, q;  cin>>n>>q;
		for(int i = 1; i <= n; i++)cin>>a[i];
		sort(a+1,a+n+1);
		se.clear();
		dfs(1,n);
		for(int i = 1; i <= q; i++){
			LL x;  cin>>x; 
			if(se.count(x))cout<<"Yes\n";
			else cout<<"No\n";
		}
	}
	return 0;
}
 
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页