E - Eating Queries CodeForces - 1676E

Timur has nn candies. The ii-th candy has a quantity of sugar equal to a_ia
i

. So, by eating the ii-th candy, Timur consumes a quantity of sugar equal to a_ia
i

.

Timur will ask you qq queries regarding his candies. For the jj-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 x_jx
j

or print -1 if it’s not possible to obtain such a quantity. In other words, you should print the minimum possible kk such that after eating kk candies, Timur consumes a quantity of sugar of at least x_jx
j

or say that no possible kk 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 tt (1 \leq t \leq 10001≤t≤1000) — the number of test cases. The description of test cases follows.

The first line contains 22 integers nn and qq (1 \leq n, q \leq 1.5\cdot10^51≤n,q≤1.5⋅10
5
) — the number of candies Timur has and the number of queries you have to print an answer for respectively.

The second line contains nn integers a_1, a_2, \dots, a_na
1

,a
2

,…,a
n

(1 \leq a_i \leq 10^41≤a
i

≤10
4
) — the quantity of sugar in each of the candies respectively.

Then qq lines follow.

Each of the next qq lines contains a single integer x_jx
j

(1 \leq x_j \leq 2 \cdot 10^91≤x
j

≤2⋅10
9
) – the quantity Timur wants to reach for the given query.

It is guaranteed that the sum of nn and the sum of qq over all test cases do not exceed 1.5 \cdot 10^51.5⋅10
5
.

Output
For each test case output qq lines. For the jj-th line output the number of candies Timur needs to eat in order to reach a quantity of sugar greater than or equal to x_jx
j

or print -1 if it’s not possible to obtain such a quantity.

Sample 1
Inputcopy Outputcopy
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>
using namespace std;
const int x = 1e6;
bool cmp(int x, int y) {
	return x > y;
}
int main(void) {
	int n;
	cin >> n;
	int l, r;
	int a[x];
	int b[x];
	for (int i = 0; i < n; i++) {
		cin >> l >> r;
		for (int j = 0; j < l; j++) {
			cin >> a[j];
		}
		sort(a, a + l,cmp);
		for (int j = 0; j <l ; j++) {
			if (j == 0) {
				b[j] = a[j];
			}
			else {
				b[j] = b[j - 1] + a[j];
			}
		}
		int k;
		for (int j = 0; j < r; j++) {
			cin >> k;
			if (k > b[l-1]) {
				cout << -1 << endl;
				continue;
			}
			int x=0, y=l-1;
			while (x < y)
			{
				int mid = x + y >> 1;
				if (b[mid] >= k) y = mid;
				else x = mid + 1;
			}
			if (k <= b[x]) cout << x+1 << "\n";
			else cout << "-1\n";
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值