#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int n;
cin >> n;
vector<int> A(n + 1);
for (int i = 1; i <= n; i++) cin >> A[i];
vector<int> diff(n + 2, 0);
vector<pair<int, int>> St;
St.reserve(2 * n + 5);
for (int i = 1; i <= 2 * n - 1; i++)
{
int x = A[(i - 1) % n + 1];
while (!St.empty() && St.back().second < x) St.pop_back();
int p = 0;
if (!St.empty())
{
p = St.back().first;
}
int L = max(1, max(i - n + 1, p + 1));
int R = min(n, i);
if (L <= R)
{
diff[L]++;
diff[R + 1]--;
}
St.push_back({i, x});
}
int ans = 0;
int akt = 0;
for (int i = 1; i <= n; i++)
{
akt += diff[i];
ans = max(ans, akt);
}
cout << ans << '\n';
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> A(n + 1); for (int i = 1; i <= n; i++) cin >> A[i]; vector<int> diff(n + 2, 0); vector<pair<int, int>> St; St.reserve(2 * n + 5); for (int i = 1; i <= 2 * n - 1; i++) { int x = A[(i - 1) % n + 1]; while (!St.empty() && St.back().second < x) St.pop_back(); int p = 0; if (!St.empty()) { p = St.back().first; } int L = max(1, max(i - n + 1, p + 1)); int R = min(n, i); if (L <= R) { diff[L]++; diff[R + 1]--; } St.push_back({i, x}); } int ans = 0; int akt = 0; for (int i = 1; i <= n; i++) { akt += diff[i]; ans = max(ans, akt); } cout << ans << '\n'; return 0; } |
English