CF1868B2.Candy Party (Hard Version)
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference is that in this version everyone must give candies to no more than one person and receive candies from no more than one person. Note that a submission cannot pass both versions of the problem at the same time. You can make hacks only if both versions of the problem are solved.
After Zhongkao examination, Daniel and his friends are going to have a party. Everyone will come with some candies.
There will be n people at the party. Initially, the i-th person has ai candies. During the party, they will swap their candies. To do this, they will line up in an arbitrary order and everyone will do the following no more than once:
- Choose an integer p (1≤p≤n) and a non-negative integer x, then give his 2x candies to the p-th person. Note that one cannot give more candies than currently he has (he might receive candies from someone else before) and he cannot give candies to himself.
Daniel likes fairness, so he will be happy if and only if everyone receives candies from no more than one person. Meanwhile, his friend Tom likes average, so he will be happy if and only if all the people have the same number of candies after all swaps.
Determine whether there exists a way to swap candies, so that both Daniel and Tom will be happy after the swaps.
这是本题的困难版本。唯一区别在于,在该版本中,每个人至多向一个人赠送糖果,且至多从一个人处接收糖果。注意:一份提交代码无法同时通过本题的两个版本。仅当两个版本均已被解决时,才允许进行 hack。
中考结束后,Daniel 和他的朋友们将举办一场聚会。每个人都将带一些糖果前来。
聚会上共有 n 人。初始时,第 i 个人拥有 ai 颗糖果。在聚会过程中,他们将交换糖果。为此,所有人将按任意顺序排成一列,然后每人至多执行一次以下操作:
- 选择一个整数 p(1≤p≤n)和一个非负整数 x,并将自己手中的 2x 颗糖果赠送给第 p 个人。注意:任何人不能赠送超过其当前所拥有的糖果数量(他可能在此前已从他人处收到糖果),且不能将糖果赠送给自身。
Daniel 喜欢公平,因此他仅当每人至多从一个人处接收糖果时才会开心。与此同时,他的朋友 Tom 喜欢平均,因此他仅当所有人在所有交换结束后拥有相同数量的糖果时才会开心。
请判断是否存在一种糖果交换方案,使得交换结束后 Daniel 和 Tom 均开心。
输入格式
The first line of input contains a single integer t (1≤t≤1000) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of people at the party.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the number of candies each person has.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105),表示派对上的人数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109),表示每个人所拥有的糖果数量。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, print "Yes" (without quotes) if exists a way to swap candies to make both Daniel and Tom happy, and print "No" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
对于每个测试用例,如果存在一种交换糖果的方式使得 Daniel 和 Tom 都开心,则输出 “Yes”(不带引号);否则输出 “No”(不带引号)。
您可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答。
输入输出样例
输入#1
6 3 2 4 3 5 1 2 3 4 5 6 1 4 7 1 5 4 2 20092043 20092043 12 9 9 8 2 4 4 3 5 1 1 1 1 6 2 12 7 16 11 12
输出#1
Yes Yes No Yes No Yes
说明/提示
In the first test case, the second person gives 1 candy to the first person, then all people have 3 candies.
In the second test case, the fourth person gives 1 candy to the second person, the fifth person gives 2 candies to the first person, the third person does nothing. And after the swaps everyone has 3 candies.
In the third test case, it's impossible for all people to have the same number of candies.
In the fourth test case, the two people do not need to do anything.
在第一个测试用例中,第二个人给第一个人 1 颗糖果,之后所有人各有 3 颗糖果。
在第二个测试用例中,第四个人给第二个人 1 颗糖果,第五个人给第一个人 2 颗糖果,第三个人不进行任何操作。交换完成后,所有人各有 3 颗糖果。
在第三个测试用例中,无法使所有人拥有相同数量的糖果。
在第四个测试用例中,这两个人无需进行任何操作。
输入解题思路,AI测评打分。不知道怎么写?