Magical Bamboos
2019-07-18 20:38 木木王韦 阅读(146) 评论(0) 收藏 举报In a magical forest, there exists N bamboos that don’t quite get cut down the way you would expect.
Originally, the height of the ith bamboo is equal to hi. In one move, you can push down a bamboo and decrease its height by one, but this move magically causes all the other bamboos to increase in height by one.
If you can do as many moves as you like, is it possible to make all the bamboos have the same height?
Input
The first line of input is T – the number of test cases.
The first line of each test case contains an integer N (1 ≤ N ≤ 105) - the number of bamboos.
The second line contains N space-separated integers hi (1 ≤ hi ≤ 105) - the original heights of the bamboos.
Output
For each test case, output on a single line "yes” (without quotes), if you can make all the bamboos have the same height, and “no” otherwise.
Example
Input
2
3
2 4 2
2
1 2
Output
yes
no
这道题的题意是如果将其中一个竹子砍一米,其他竹子都会长一米,看是否有可能将他们变得一样高。
我唯一想到的就是减一米,加一米,那么两者之间的差距一定得是个偶数,结果其实就是只有这一个判断条件。
#include<iostream>
#include<algorithm>
using namespace std;
int t;
int m;
int a[100005];
int main(){
cin>>t;
while(t--){
cin>>m;
for(int i=0;i<m;i++){
cin>>a[i];
}
sort(a,a+m);
int flag=0;
for(int i=1;i<m;i++){
if((a[i]-a[i-1])%2!=0){
flag=1;
}
}
if(flag){
cout<<"no"<<endl;
}
else cout<<"yes"<<endl;
}
return 0;
}
浙公网安备 33010602011771号