#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr); ios_base::sync_with_stdio(false);
int n; cin >> n;
vector<int> V(n);
for(int i = 0; i < n; i++) {
cin >> V[i];
}
vector<int> suf(n+1, 0), sufval(n+1, -1);
vector<int> S;
for(int i = n-1; i >= 0; i--) {
while(!S.empty() && S[S.size()-1] <= V[i]) S.pop_back();
S.push_back(V[i]);
suf[i] = S.size();
sufval[i] = S[0];
}
S.clear();
int best = 0;
int cutoffId = 0; // Ostatnia pozycja mniejsza od sufiksu
S.push_back(-100);
for(int i = 0; i < n; i++) {
if(S.empty() || S[S.size()-1] < V[i]) S.push_back(V[i]);
while(cutoffId >= 0 && S[cutoffId] > sufval[i+1]) cutoffId--;
while(cutoffId < S.size()-1 && S[cutoffId+1] <= sufval[i+1]) cutoffId++;
if(best < S.size()-cutoffId-1+suf[i+1]) {
best = S.size()-cutoffId-1+suf[i+1];
}
}
cout << best << '\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 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> V(n); for(int i = 0; i < n; i++) { cin >> V[i]; } vector<int> suf(n+1, 0), sufval(n+1, -1); vector<int> S; for(int i = n-1; i >= 0; i--) { while(!S.empty() && S[S.size()-1] <= V[i]) S.pop_back(); S.push_back(V[i]); suf[i] = S.size(); sufval[i] = S[0]; } S.clear(); int best = 0; int cutoffId = 0; // Ostatnia pozycja mniejsza od sufiksu S.push_back(-100); for(int i = 0; i < n; i++) { if(S.empty() || S[S.size()-1] < V[i]) S.push_back(V[i]); while(cutoffId >= 0 && S[cutoffId] > sufval[i+1]) cutoffId--; while(cutoffId < S.size()-1 && S[cutoffId+1] <= sufval[i+1]) cutoffId++; if(best < S.size()-cutoffId-1+suf[i+1]) { best = S.size()-cutoffId-1+suf[i+1]; } } cout << best << '\n'; return 0; } |
English