This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
using ari2 = array<int, 2>;
int sequence(int N, vector<int> A) {
if (N <= 2000) {
int ans = 1;
for (int i = 0; i < N; i++) {
vector<int> cnt(N+1, 0);
multiset<ari2> stuff;
cnt[A[i]]++;
stuff.insert({A[i], -i});
auto it = stuff.begin();
for (int j = i+1; j < N; j++) {
stuff.insert({A[j], -j});
cnt[A[j]]++;
if (ari2{A[j], -j} < *it && (j - i & 1)) {
it--;
} else if (ari2{A[j], -j} > *it && !(j - i & 1)) {
it++;
}
ans = max(ans, cnt[(*it)[0]]);
}
}
return ans;
}
int middle = max_element(A.begin(), A.end()) - A.begin();
bool mountain = true;
for (int i = middle - 1; i >= 0 && mountain; i--) {
mountain &= A[i] <= A[i+1];
}
for (int i = middle + 1; i < N && mountain; i++) {
mountain &= A[i] <= A[i-1];
}
if (mountain) {
vector<int> inds[N];
for (int i = 0; i < N; i++) {
inds[A[i]-1].push_back(i);
}
int ans = 0;
for (int ii = 0; ii < N; ii++) {
if (inds[ii].size() < 2) {
ans = max(ans, (int)inds[ii].size());
continue;
}
int l1 = inds[ii][0], r1 = -1, l2 = -1, r2 = inds[ii].back();
for (int i = 1; i < inds[ii].size(); i++) {
if (inds[ii][i-1]+1 < inds[ii][i]) {
r1 = inds[ii][i-1];
l2 = inds[ii][i];
break;
}
}
if (r1 < 0) {
ans = max(ans, r2 - l1 + 1);
} else {
int gap = l2 - r1 - 1;
if (gap > r1 - l1 + 1 + r2 - l2 + 1) {
if (l1 + N - r2 - 1 >= gap - (r1 - l1 + 1 + r2 - l2 + 1)) {
ans = max(ans, r1 - l1 + 1 + r2 - l2 + 1);
}
} else {
ans = max(ans, r1 - l1 + 1 + r2 - l2 + 1);
}
}
}
return ans;
}
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:18:36: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (ari2{A[j], -j} < *it && (j - i & 1)) {
| ~~^~~
sequence.cpp:20:44: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | } else if (ari2{A[j], -j} > *it && !(j - i & 1)) {
| ~~^~~
sequence.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1; i < inds[ii].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
sequence.cpp:73:1: warning: control reaches end of non-void function [-Wreturn-type]
73 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |