IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> B. Vlad and Candies -> 正文阅读

[数据结构与算法]B. Vlad and Candies

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were?nn?types of candies, there are?aiai?candies of the type?ii?(1≤i≤n1≤i≤n).

Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose?any?of them). To get the maximum pleasure from eating, Vlad?does not want?to eat two candies of the same type in a row.

Help him figure out if he can eat all the candies without eating two identical candies in a row.

Input

The first line of input data contains an integer?tt?(1≤t≤1041≤t≤104) — the number of input test cases.

The following is a description of?tt?test cases of input, two lines for each.

The first line of the case contains the single number?nn?(1≤n≤2?1051≤n≤2?105) — the number of types of candies in the package.

The second line of the case contains?nn?integers?aiai?(1≤ai≤1091≤ai≤109) — the number of candies of the type?ii.

It is guaranteed that the sum of?nn?for all cases does not exceed?2?1052?105.

Output

Output?tt?lines, each of which contains the answer to the corresponding test case of input. As an answer, output "YES" if Vlad can eat candy as planned, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Example

input

Copy

6
2
2 3
1
2
5
1 6 2 4 3
4
2 2 2 1
3
1 1000000000 999999999
1
1

output

Copy

YES
NO
NO
YES
YES
YES

Note

In the first example, it is necessary to eat sweets in this order:

  • a candy of the type?22, it is the most frequent, now?a=[2,2]a=[2,2];
  • a candy of the type?11, there are the same number of candies of the type?22, but we just ate one, now?a=[1,2]a=[1,2];
  • a candy of the type?22, it is the most frequent, now?a=[1,1]a=[1,1];
  • a candy of the type?11, now?a=[0,1]a=[0,1];
  • a candy of the type?22, now?a=[0,0]a=[0,0]?and the candy has run out.

In the second example, all the candies are of the same type and it is impossible to eat them without eating two identical ones in a row.

In the third example, first of all, a candy of the type?22?will be eaten, after which this kind will remain the only kind that is the most frequent, and you will have to eat a candy of the type?22?again.

解题说明:此题是一道模拟题,只需要判断每次不从相同的糖果中取即可。

#include <stdio.h>

int main()
{
	int nCases;
	int n;
	int most, premost;
	int buf;
	scanf("%d", &nCases);
	while (nCases-- > 0) 
	{
		scanf("%d", &n);
		most = 0;
		premost = 0;
		while (n-- > 0)
		{
			scanf("%d", &buf);
			if (buf >= most) 
			{
				premost = most;
				most = buf;
			}
			else if (buf > premost)
			{
				premost = buf;
			}
		}
		if (most - premost <= 1)
		{
			puts("YES");
		}
		else
		{
			puts("NO");
		}
	}
	return 0;
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-04-18 18:07:50  更:2022-04-18 18:10:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 7:24:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码