#include <bits/stdc++.h>
#include "sequence.h"
using namespace std;
int sequence(int n, vector<int> a){
if(n <= 2000){
int occ[n + 1], ans = 0;
for(int i = 0; i < n; i++){
memset(occ, 0, sizeof(occ));
multiset<int> l, r;
for(int j = i; j < n; j++){
occ[a[j]]++;
if(r.empty() || a[j] <= *r.begin()) l.insert(a[j]);
else r.insert(a[j]);
if(l.size() > r.size() + 1){
r.insert(*--l.end());
l.erase(--l.end());
}
if(r.size() > l.size()){
l.insert(*r.begin());
r.erase(r.begin());
}
ans = max(ans, occ[*--l.end()]);
if(l.size() == r.size()) ans = max(ans, occ[*r.begin()]);
}
}
return ans;
}
map<int, int> asc, desc;
bool flag = true;
for(int i = 0; i < n; i++){
if(flag) asc[a[i]]++;
else desc[a[i]]++;
if(i && a[i] <= a[i - 1]) flag = false;
}
for(auto p : asc){
auto p2 = lower_bound(a.begin(), a.end(), p.first) - a.begin();
if(p2 >= n / 2) ans = max(ans, p.second + desc[p.first]);
}
for(auto p : desc){
auto p2 = upper_bound(a.begin(), a.end(), p.first) - a.begin();
if(p2 >= n / 2) ans = max(ans, p.second + desc[p.first]);
}
return ans;
}
Compilation message
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:39:21: error: 'ans' was not declared in this scope; did you mean 'abs'?
39 | if(p2 >= n / 2) ans = max(ans, p.second + desc[p.first]);
| ^~~
| abs
sequence.cpp:43:21: error: 'ans' was not declared in this scope; did you mean 'abs'?
43 | if(p2 >= n / 2) ans = max(ans, p.second + desc[p.first]);
| ^~~
| abs
sequence.cpp:45:10: error: 'ans' was not declared in this scope; did you mean 'abs'?
45 | return ans;
| ^~~
| abs