fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. PrintWriter out = new PrintWriter(System.out,true);
  14. //StringTokenizer st = new StringTokenizer(br.readLine());
  15. int t = Integer.parseInt(br.readLine());
  16. while(t-->0){
  17. int n = Integer.parseInt(br.readLine());
  18. StringTokenizer st = new StringTokenizer(br.readLine());
  19. long[] a = new long[n];
  20. for(int i =0;i<n;i++){
  21. a[i] = Long.parseLong(st.nextToken());
  22. }
  23. boolean yes = true;
  24. int[] mark = new int[n];
  25. mark[0] = 0;
  26. for(int i =0;i<n-1;i++){
  27. if(a[i]<a[i+1] && mark[i+1] == 1){
  28. yes = false;
  29. break;
  30. }
  31. else if(a[i]>a[i+1]){
  32. a[i] = a[i]^a[i+1];
  33. a[i+1] = a[i]^a[i+1];
  34. a[i] = a[i]^a[i+1];
  35. mark[i+1] = 1;
  36. }
  37. }
  38. for(int i =0;i<n-1;i++){
  39. if(a[i]>a[i+1]){
  40. yes = false;
  41. break;
  42. }
  43. }
  44. if(yes)out.println("YES");
  45. else out.println("NO");
  46. }
  47. }
  48. }
Success #stdin #stdout 0.1s 320512KB
stdin
2
3
1 2 3
5
2 4 1 3 5
stdout
YES
NO