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<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int n, K, w[3010], C[3010], S[3010], res, CC[3010], IT[3010];
void Do() {
int i, j;
for (i = 1; i < n; i++) {
int b = i;
for (j = 1; j <= K; j++)C[j] = 0;
set<int>Set;
C[w[b]]++;
Set.insert(w[b]);
while (b > 1 && w[b - 1] >= w[b]) {
b--;
C[w[b]]++;
Set.insert(w[b]);
}
for (j = 1; j <= K; j++){
S[j] = C[j];
S[j] += S[j - 1];
}
int small = -1, smallc = 0;
for (j = i + 1; j <= n; j++) {
if (w[i] > w[j]) {
if (small != -1) {
if (small == w[j])smallc++;
else break;
}
else {
small = w[j]; smallc = 1;
}
}
else {
if (!C[w[j]])Set.insert(w[j]);
C[w[j]]--;
if (!C[w[j]])Set.erase(w[j]);
}
int last = K;
if (!Set.empty()) last = *Set.begin();
res = max(res,S[last - 1] * 2 + (S[last] - S[last - 1] + min(0, -C[last]))*2 + smallc);
}
}
for (i = 2; i <= n+n; i++) {
int b = i / 2 + 1, e = i - i / 2 - 1;
while (b > 1 && e < n && w[b - 1] == w[e + 1])b--, e++;
int bb = b-1;
for (j = 1; j <= K; j++)C[j] = 0;
set<int>Set;
res = max(res, e - b + 1);
if (b == 1) continue;
for (j = bb; j >= 1; j--) {
if (j != bb && w[j] < w[j + 1])break;
C[w[j]]++;
Set.insert(w[j]);
}
for (j = 1; j <= K; j++) {
S[j] = C[j];
S[j] += S[j - 1];
}
for (j = e + 1; j <= n; j++) {
if (w[bb] > w[j])break;
if (!C[w[j]])Set.insert(w[j]);
C[w[j]]--;
if (!C[w[j]])Set.erase(w[j]);
int last = K;
if (!Set.empty()) last = *Set.begin();
res = max(res, S[last - 1] * 2 + (S[last] - S[last - 1] + min(0, -C[last]))*2 + e-b+1);
}
}
}
int main() {
int i, j;
scanf("%d%d", &n, &K);
for (i = 1; i <= n; i++) {
scanf("%d", &w[i]);
C[w[i]]++;
res = max(res, C[w[i]]);
}
Do();
for (i = 1; i <= n; i++)reverse(w + 1, w + n + 1);
Do();
printf("%d\n", res);
}
Compilation message (stderr)
aaqqz.cpp: In function 'int main()':
aaqqz.cpp:73:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
aaqqz.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &K);
~~~~~^~~~~~~~~~~~~~~~
aaqqz.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &w[i]);
~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |