B. Good Arrays
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array of positive integers a
of length n.
Let's call an array of positive integers b of length n good if:ai≠bi for all i from 1 to n,
a1+a2+…+an=b1+b2+…+bn.
Does a good array exist ?
Input
Each test contains multiple test cases. The first line of input contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array a.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, output "YES" (without quotes) if there exists a good array, and "NO" (withoutquotes) 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.
Example
input
6
3
6 1 2
2
1 1
4
3 1 2 4
1
17
5
1 2 1 1 1
3
618343152 819343431 1000000000
output
YES
NO
YES
NO
NO
YES
Note
In the first test case, a possible good array is [3,3,3]. Some examples of not good arrays are:[8,0,1] — the array does not consist of only positive integers,
[5,2,4] — the array does not have the same sum as the given array,
[5,2,2] — the third element is equal to the third element of the given array.
In the second test case, [1,1] is the only array of positive integers of length 2 that has the sum of it's elements equal to 2. Since [1,1] is not a good array, the answer is "NO".
WORRY IDEA
special situation
1 , length is only 1
2 , consist twice or more 1 in a array(is not absolutely right)
!cnt(how often)
手动模拟
if n=3
1 2 2 -> 3 1 1
n
1 ((n+1)/2)
2 ((n-1)/2)
(n+1)/2+(n-1) = 3/2n - 1/2
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5+10;
int a[N],b[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
ll sum = 0;
for(int i = 0;i<n;i++) {
cin>>a[i];
sum+=a[i];
}
if(n==1) cout<<"NO"<<endl;
else {
if(sum<=1.5*n-0.5) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
return 0;
}
存在的问题:
目前为止
正确思路
1,每个大于1的数都能变成1
2,只需要比较sum(ai-1)与cnt(1的个数)的大小代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5+10;
int a[N],b[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
ll sum = 0,cnt = 0;
for(int i = 0;i<n;i++) {
cin>>a[i];
sum+=a[i]-1;
if(a[i]==1) cnt++;
}
if(n==1) cout<<"NO"<<endl;
else {
if(sum>=cnt) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}