#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <string>
#include <queue>
#include <numeric>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
struct mq {
deque<pii> q, m;
int push(pii x) {
q.push_back(x);
while (!m.empty() && m.back() < x) {
m.pop_back();
}
int r = -1;
if (!m.empty()) {
r = -m.back().second;
}
m.push_back(x);
return r;
}
pii pop() {
auto x = q.front();
q.pop_front();
if (m.front() == x) {
m.pop_front();
}
return x;
}
int mx() {
return m.front().first;
}
};
int main() {
int n;
cin >> n;
vector<int> cnt(2 * n);
mq Q;
int res = 0;
int mx = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
res += a > mx;
mx = max(mx, a);
int t = Q.push(pii{a, -i});
if (t != -1) {
cnt[t]++;
}
}
int cur = res;
for (int i = 0; i < n; i++) {
cur--;
cur += cnt[i];
pii p = Q.pop();
cur += p.first > Q.mx();
res = max(res, cur);
int t = Q.push(pii{p.first, p.second - n});
if (t != -1) {
cnt[t]++;
}
}
cout << res << endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <set> #include <cmath> #include <string> #include <queue> #include <numeric> using namespace std; using ll = long long; using pii = pair<int, int>; struct mq { deque<pii> q, m; int push(pii x) { q.push_back(x); while (!m.empty() && m.back() < x) { m.pop_back(); } int r = -1; if (!m.empty()) { r = -m.back().second; } m.push_back(x); return r; } pii pop() { auto x = q.front(); q.pop_front(); if (m.front() == x) { m.pop_front(); } return x; } int mx() { return m.front().first; } }; int main() { int n; cin >> n; vector<int> cnt(2 * n); mq Q; int res = 0; int mx = 0; for (int i = 0; i < n; i++) { int a; cin >> a; res += a > mx; mx = max(mx, a); int t = Q.push(pii{a, -i}); if (t != -1) { cnt[t]++; } } int cur = res; for (int i = 0; i < n; i++) { cur--; cur += cnt[i]; pii p = Q.pop(); cur += p.first > Q.mx(); res = max(res, cur); int t = Q.push(pii{p.first, p.second - n}); if (t != -1) { cnt[t]++; } } cout << res << endl; return 0; } |
English