7.C - Eating Queries

文章描述了一个关于Timur吃糖果的问题,他有不同糖量的糖果,需要在一系列独立的查询中找到最少需要吃多少糖果才能达到或超过特定糖量。程序需要找出每个查询的答案,或者表示不可能达到。
摘要由CSDN通过智能技术生成

Timur has n� candies. The i�-th candy has a quantity of sugar equal to ai��. So, by eating the i�-th candy, Timur consumes a quantity of sugar equal to ai��.

Timur will ask you q� queries regarding his candies. For the j�-th query you have to answer what is the minimum number of candies he needs to eat in order to reach a quantity of sugar greater than or equal to xj�� or print -1 if it's not possible to obtain such a quantity. In other words, you should print the minimum possible k� such that after eating k� candies, Timur consumes a quantity of sugar of at least xj�� or say that no possible k� exists.

Note that he can't eat the same candy twice and queries are independent of each other (Timur can use the same candy in different queries).

Input

The first line of input contains a single integer t� (1≤t≤10001≤�≤1000)  — the number of test cases. The description of test cases follows.

The first line contains 22 integers n� and q� (1≤n,q≤1.5⋅1051≤�,�≤1.5⋅105) — the number of candies Timur has and the number of queries you have to print an answer for respectively.

The second line contains n� integers a1,a2,…,an�1,�2,…,�� (1≤ai≤1041≤��≤104) — the quantity of sugar in each of the candies respectively.

Then q� lines follow.

Each of the next q� lines contains a single integer xj�� (1≤xj≤2⋅1091≤��≤2⋅109) – the quantity Timur wants to reach for the given query.

It is guaranteed that the sum of n� and the sum of q� over all test cases do not exceed 1.5⋅1051.5⋅105.

Output

For each test case output q� lines. For the j�-th line output the number of candies Timur needs to eat in order to reach a quantity of sugar greater than or equal to xj�� or print -1 if it's not possible to obtain such a quantity.

Sample 1

InputcopyOutputcopy
3
8 7
4 3 3 1 1 4 5 9
1
10
50
14
15
22
30
4 1
1 2 3 4
3
1 2
5
4
6
1
2
-1
2
3
4
8
1
1
-1

Note

For the first test case:

For the first query, Timur can eat any candy, and he will reach the desired quantity.

For the second query, Timur can reach a quantity of at least 1010 by eating the 77-th and the 88-th candies, thus consuming a quantity of sugar equal to 1414.

For the third query, there is no possible answer.

For the fourth query, Timur can reach a quantity of at least 1414 by eating the 77-th and the 88-th candies, thus consuming a quantity of sugar equal to 1414.

For the second test case:

For the only query of the second test case, we can choose the third candy from which Timur receives exactly 33 sugar. It's also possible to obtain the same answer by choosing the fourth candy.

#include<iostream>
#include<algorithm>
#define endl '\n'
#define ll long long
//#define int ll
using namespace std;
const int N = 1e6 + 7;
bool cmp(int a, int b)
{
	return a > b;
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		int a[150009] = { 0 };
		int sum[150009] = { 0 };
		int n, q;
		cin >> n >> q;
		for (int i = 1;i <= n;i++) cin >> a[i];
		sort(a + 1, a + 1 + n, cmp);
		for (int i = 1;i <= n;i++)
		{
			sum[i] = sum[i - 1] + a[i];
		}
		for (int i = 1;i <= q;i++)
		{
			int x;
			cin >> x;
			int l = 1, r = n;
			while (l < r)
			{
				int mid = (l + r) / 2;
				if (sum[mid] >= x) r = mid;
				else l = mid + 1;
			}
			if (sum[l] >= x) cout << l << endl;
			else cout << "-1" << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值