#include <iostream>
using namespace std;
const int N = 3e5 + 5, M = 1e7 + 5;
int qmax[M], qmin[M], h1, h2, t1, t2, n, a[N], res, p;
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
h1 = h2 = 1, t1 = t2 = 0, a[n + 1] = -0x3f3f3f3f;
while (++p <= n + 1)
{
if (a[p] < a[qmin[h1]] && t1 >= h1 && t2 >= h2)
{
res++, p = qmax[h2], h1 = t1 + 1, h2 = t2 + 1;
continue;
}
while (h1 <= t1 && a[qmin[t1]] >= a[p])
t1--;
while (h2 <= t2 && a[qmax[t2]] <= a[p])
t2--;
qmin[++t1] = p, qmax[++t2] = p;
}
cout << res << endl;
}
不是一次了,刚刚写的另一个单调队列也是RE,不知道为什么...q数组明明已经开的很大了