Y - Ball
如图所示,容器中间有一枢轴,下方分为两路。容器上方开口,从1到10连续编号的小球从容器开口A放入。通过调整枢轴E的方向,可以使小球经过D而落入左侧B筒或者右侧C筒。现给出从A放入小球的顺序,请你判断能否最终小球落入B和C时,号码大的球总是位于号码小的球的上侧。如果可能则在一行中输出”YES”,否则输出 “NO”
Input
第一行一个整数N(N<1000)
接下来有N行,每行10个由空格分隔的[1,10]区间内的整数,分别代表10个小球放入A筒的顺序
Output
对每组数据,输出一行YES或NO
Sample
Sample Input
2
3 1 4 2 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
Output for the Sample Input
YES
NO
贪心,每次找到符合条件且是两边最上层的数最大的那个方向加进去
无需用ArrayList 直接用a/b表示最上层就可以了
无需DFS
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[10];
while(n--!=0) {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int a=0,b=0;
int res = 1;
for (int i = 0; i < arr.length; i++) {
int ans = 0;
if(a<arr[i] && ((a>=b)||b>=arr[i])) {
a = arr[i];
ans=1;
continue;
}
if(b<arr[i] && ((b>=a)||a>=arr[i])) {
b = arr[i];
ans=1;
continue;
}
if(ans==0) {
res=0;break;
}
}
if(res==1)
System.out.println("YES");
else
System.out.println("NO");
}
}
}