rt。
Wrong Answer on test#2
#include <algorithm>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n + 2), prv(n + 2), nxt(n + 2);
vector<bool> vis(n + 2, true);
for (int i = 1; i <= n; i++)
cin >> a[i], prv[i] = i - 1, nxt[i] = i + 1;
priority_queue<pair<int, pair<int, int>>> q;
for (int i = 1; i <= n; i = nxt[i])
if (nxt[i] <= n && abs(a[i] - a[nxt[i]]) == 1)
q.emplace(max(a[i], a[nxt[i]]), make_pair(i, nxt[i]));
int ans = 0;
while (!q.empty())
{
int x = q.top().second.first, y = q.top().second.second;
q.pop();
if (!vis[x] || !vis[y] || nxt[x] != y || prv[y] != x)
continue;
ans++;
if (max(a[x], a[y]) == a[x])
{
nxt[prv[x]] = y;
prv[y] = prv[x];
vis[x] = false;
int u = prv[x], v = y;
if (u >= 1 && abs(a[u] - a[v]) == 1)
q.emplace(max(a[u], a[v]), make_pair(u, v));
}
else
{
prv[nxt[y]] = x;
nxt[x] = nxt[y];
vis[y] = false;
int u = x, v = nxt[y];
if (v <= n && abs(a[u] - a[v]) == 1)
q.emplace(max(a[u], a[v]), make_pair(u, v));
}
}
cout << (ans == n - 1 && a[nxt[0]] == 0 ? "YES" : "NO") << '\n';
}
return 0;
}