B. Social Distance

题目:
m chairs are arranged in a circle sequentially. The chairs are numbered from 0 to m−1. n people want to sit in these chairs. The i-th of them wants at least a[i] empty chairs both on his right and left side.

More formally, if the i-th person sits in the j-th chair, then no one else should sit in the following chairs: (j−a[i])%m, (j−a[i]+1)%m, … (j+a[i]−1)%m, (j+a[i])%m.

Decide if it is possible to sit down for all of them, under the given limitations.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤50000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n and m (2≤n≤100000, 1≤m≤1000000000) — the number of people and the number of chairs.

The next line contains n integers, a1, a2, … an (1≤ai≤1000000000) — the minimum number of empty chairs, on both sides of the i-th person.

It is guaranteed that the sum of n over all test cases will not exceed 100000.

Output
For each test case print “YES” (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and “NO” (without quotes) otherwise.

You may print every letter in any case you want (so, for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be recognized as positive answers).

Example
input

6
3 2
1 1 1
2 4
1 1
2 5
2 1
3 8
1 2 1
4 12
1 2 1 3
4 19
1 2 1 3

output

NO
YES
NO
YES
NO
YES

Note
Test case 1: n>m, so they can not sit down.

Test case 2: the first person can sit 2-nd and the second person can sit in the 0-th chair. Both of them want at least 1 empty chair on both sides, chairs 1 and 3 are free, so this is a good solution.

Test case 3: if the second person sits down somewhere, he needs 2 empty chairs, both on his right and on his left side, so it is impossible to find a place for the first person, because there are only 5 chairs.

Test case 4: they can sit in the 1-st, 4-th, 7-th chairs respectively.
AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];

bool cmp(int a, int b) {
	return a > b; 
}
int main() {
	int t, n;
	ll m;
	cin >> t;
	while (t--) {
		memset(a, 0, sizeof a);
		cin >> n >> m;
		for (int i = 1; i <= n; i++) cin >> a[i];
		if (n > m) {
			cout << "NO" << endl;
			continue; 
		} else {
			sort(a + 1, a + n + 1, cmp);
			ll sum = 0;
			for (int i = 1; i <= n; i++) {
				if (i == 1) {
					sum += 2 * a[i] + 1;
				} else if (i < n) {
					sum += a[i] + 1;
				} else {
					sum ++;
				}
			}
			if (sum > m) cout << "NO" << endl;
			else cout << "YES" << endl; 
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值