이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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});
ari2 med = {A[i], -i};
for (int j = i+1; j < N; j++) {
stuff.insert({A[j], -j});
cnt[A[j]]++;
if (ari2{A[j], -j} < med && (j - i & 1)) {
med = *prev(stuff.find(med));
} else if (ari2{A[j], -j} > med && !(j - i & 1)) {
med = *next(stuff.find(med));
}
ans = max(ans, cnt[med[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;
}
}
컴파일 시 표준 에러 (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} < med && (j - i & 1)) {
| ~~^~~
sequence.cpp:20:44: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | } else if (ari2{A[j], -j} > med && !(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... |