fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ff(i , a , b) for(int i = a; i <= int(b); i++)
  5. #define REP(i, a , b) for(int i = a; i >= int(b); i--)
  6. #define MP make_pair
  7. #define PB push_back
  8. typedef long long ll;
  9. typedef pair<int ,int> pi;
  10. const ll MAXN = 1e6 + 7;
  11. const int INF = 1e9 + 7;
  12. vector<int> v;
  13. set<int> check;
  14. void solve()
  15. {
  16. v.clear();
  17. check.clear();
  18. int n;
  19. scanf("%d", &n);
  20. v.resize(n);
  21. ff(i , 0 ,n - 1) scanf("%d", &v[i]);
  22. if (n == 1)
  23. {
  24. printf("YES\n");
  25. return;
  26. }
  27. check.insert(v[0]);
  28. check.insert(INF);
  29. check.insert(-INF);
  30. ff(i , 1 , n - 1)
  31. {
  32. if (v[i] == v[i-1]) continue;
  33. if (v[i] > v[i-1])
  34. {
  35. auto it = upper_bound(check.begin() , check.end() , v[i-1]);
  36. if (*it < v[i])
  37. {
  38. printf("NO\n");
  39. return;
  40. }
  41. }
  42. if (v[i] < v[i-1])
  43. {
  44. auto it = upper_bound(check.begin() , check.end() , v[i]);
  45. if (*it != v[i-1])
  46. {
  47. printf("NO\n");
  48. return;
  49. }
  50.  
  51. }
  52. check.insert(v[i]);
  53. }
  54. printf("YES\n");
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60. //ios_base::sync_with_stdio(0);
  61. //cin.tie(0);cout.tie(0);
  62. //freopen("X.inp" , "r" , stdin);
  63. //freopen("Y.out" , "w" , stdout);
  64. int t;
  65. scanf("%d", &t);
  66. while(t--) solve();
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0s 5604KB
stdin
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
stdout
NO
YES
NO
NO
NO