import java.util.*;
public class test1 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int group = scanner.nextInt();
while(group-- > 0){
int num = scanner.nextInt();
List<Integer> tree = new ArrayList<>();
for(int i = 0; i < num; i++){
tree.add(scanner.nextInt());
}
int n = Math.min(11, num / 2);
int res = digui(tree, n, true);
if(res != -1){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
public static int digui(List<Integer> tree, int n, boolean t){
if(tree.isEmpty()){
return 0;
}
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
int root = tree.get(0);
int length = tree.size();
for(int i = 1; i < length; i++){
int cur = tree.get(i);
if(cur < root){
left.add(cur);
}else{
right.add(cur);
}
}
int leftN = digui(left, n, true);
int rightN = digui(right, n, false);
// int height = Math.max(leftN, rightN) + 1;
if(Math.max(leftN, rightN) - Math.min(leftN, rightN) >= n || leftN == -1 || rightN == -1){
return -1;
}else{
if(t){
return leftN + 1;
}else{
return rightN + 1;
}
}
}
}
有没有大佬帮我看看这样写错在哪儿了?AC 0%
全部评论
(3) 回帖