fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int t;
  6. cin >> t;
  7.  
  8. while (t--) {
  9. int n;
  10. cin >> n;
  11.  
  12. vector<int> p(n);
  13. unordered_map<int, int> index_map;
  14.  
  15. for (int i = 0; i < n; i++) {
  16. cin >> p[i];
  17. index_map[p[i]] = i;
  18. }
  19.  
  20. bool can_sort = true;
  21.  
  22. vector<int> sorted_p = p;
  23. sort(sorted_p.begin(), sorted_p.end());
  24.  
  25. for (int i = 0; i < n - 1; i++) {
  26. if (p[i] > p[i + 1] && abs(p[i] - p[i + 1]) != 1) {
  27. can_sort = false;
  28. break;
  29. }
  30. }
  31.  
  32. cout << (can_sort ? "YES" : "NO") << endl;
  33. }
  34. }
  35.  
  36. int main() {
  37. solve();
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5268KB
stdin
2
4
2 1 3 4
4
4 2 3 1
stdout
YES
NO