#include <stdio.h>
#include <string.h>
#define N 3000
#define C 3000
int max(int a, int b) { return a > b ? a : b; }
int pp[N][C], dp[N][N];
int count(int l, int r, int a, int b) {
return l > r || a > b ? 0 : pp[r][b] - (a == 0 ? 0 : pp[r][a - 1]) - (l == 0 ? 0 : pp[l - 1][b] - (a == 0 ? 0 : pp[l - 1][a - 1]));
}
int solve(int *aa, int n, int c) {
int i, i_, j_, j, j1, j2, ij, a, k, ans;
for (i = 0; i < n; i++)
memset(pp[i], 0, c * sizeof *pp[i]);
for (i = 0; i < n; i++)
pp[i][aa[i]]++;
for (i = 0; i < n; i++)
for (a = 1; a < c; a++)
pp[i][a] += pp[i][a - 1];
for (a = 0; a < c; a++)
for (i = 1; i < n; i++)
pp[i][a] += pp[i - 1][a];
for (i = 0; i < n; i++)
for (j = i; j < n; j++)
dp[i][j] = aa[i] != aa[j] ? 0 : (i > 0 && j + 1 < n ? dp[i - 1][j + 1] : 0) + 1;
ans = 0;
for (a = 0; a < c; a++)
ans = max(ans, count(0, n - 1, a, a));
for (ij = 0; ij <= (n - 1) + (n - 1); ij++) {
i = ij / 2, j = ij - i;
while (i >= 0 && j < n && aa[i] == aa[j])
i--, j++;
ans = max(ans, j - i - 1);
for (i_ = i, j_ = j, j1 = j, j2 = n - 1; i_ >= 0 && j_ < n; i_--, j_++) {
if (i_ < i && aa[i_] < aa[i_ + 1])
break;
k = count(i_, i, aa[i_], aa[i_]);
while (j1 < n && count(j, j1, aa[i_], aa[i_]) < k)
j1++;
k = count(i_, i, 0, aa[i_] - 1);
while (j2 >= j && count(j, j2, 0, aa[i_] - 1) > k)
j2--;
if (j1 <= j2) {
ans = max(ans, j_ - i_ + 1);
if (j1 <= j_ && j_ <= j2)
ans = max(ans, j_ - i_ + 1 + (i_ > 0 && j_ + 1 < n ? dp[i_ - 1][j_ + 1] * 2 : 0));
}
}
}
for (i = 0; i + 1 < n; i++) {
a = -1;
for (j = i + 1; j < n; j++)
if (aa[j] < aa[i]) {
a = aa[j];
break;
}
for (i_ = i, j1 = i + 1, j2 = n - 1; i_ >= 0; i_--) {
if (i_ < i && aa[i_] < aa[i_ + 1])
break;
k = count(i_, i, aa[i_], aa[i_]);
while (j1 < n && count(i + 1, j1, aa[i_], aa[i_]) < k)
j1++;
k = count(i_, i, 0, aa[i_] - 1);
while (j2 > i && count(i + 1, j2, 0, aa[i_] - 1) - (a == -1 ? 0 : count(i + 1, j2, a, a)) > k)
j2--;
if (j1 <= j2)
ans = max(ans, (i - i_ + 1) * 2 + (a == -1 ? 0 : count(i + 1, j2, a, a)));
}
for (i_ = i, j = i + 1; j < n; j++) {
if (aa[j] < aa[i]) {
if (aa[j] != a)
break;
} else {
k = count(i + 1, j, aa[j], aa[j]);
while (i_ >= 0 && count(i_, i, aa[j], aa[j]) < k) {
i_--;
if (i_ < 0 || aa[i_] < aa[i_ + 1]) {
i_ = -1;
break;
}
}
}
if (i_ >= 0 && i - i_ + 1 == j - i - (a == -1 ? 0 : count(i + 1, j, a, a)))
ans = max(ans, j - i_ + 1 + (i_ > 0 && j + 1 < n ? dp[i_ - 1][j + 1] * 2 : 0));
}
}
return ans;
}
int main() {
static int aa[N];
int n, c, i, j, r, tmp, ans;
scanf("%d%d", &n, &c);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]), aa[i]--;
ans = 0;
for (r = 0; r < 2; r++) {
ans = max(ans, solve(aa, n, c));
for (i = 0, j = n - 1; i < j; i++, j--)
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
for (i = 0; i < n; i++)
aa[i] = c - 1 - aa[i];
}
printf("%d\n", ans);
return 0;
}
Compilation message
aaqqz.c: In function 'main':
aaqqz.c:99:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d%d", &n, &c);
| ^~~~~~~~~~~~~~~~~~~~~
aaqqz.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
684 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
680 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
684 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
19 |
Correct |
1 ms |
672 KB |
Output is correct |
20 |
Correct |
1 ms |
768 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
680 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
684 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
680 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
684 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
19 |
Correct |
1 ms |
672 KB |
Output is correct |
20 |
Correct |
1 ms |
768 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
680 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1008 ms |
63088 KB |
Output is correct |
39 |
Correct |
838 ms |
63104 KB |
Output is correct |
40 |
Correct |
782 ms |
63052 KB |
Output is correct |
41 |
Correct |
750 ms |
63076 KB |
Output is correct |
42 |
Correct |
745 ms |
63084 KB |
Output is correct |
43 |
Correct |
693 ms |
63076 KB |
Output is correct |
44 |
Correct |
699 ms |
63104 KB |
Output is correct |
45 |
Correct |
952 ms |
63176 KB |
Output is correct |
46 |
Correct |
938 ms |
63080 KB |
Output is correct |
47 |
Correct |
981 ms |
63076 KB |
Output is correct |
48 |
Correct |
968 ms |
63072 KB |
Output is correct |
49 |
Correct |
987 ms |
63080 KB |
Output is correct |
50 |
Correct |
938 ms |
63080 KB |
Output is correct |
51 |
Correct |
999 ms |
63076 KB |
Output is correct |
52 |
Correct |
934 ms |
63044 KB |
Output is correct |
53 |
Correct |
664 ms |
63080 KB |
Output is correct |
54 |
Correct |
695 ms |
63172 KB |
Output is correct |
55 |
Correct |
903 ms |
63080 KB |
Output is correct |
56 |
Correct |
953 ms |
63036 KB |
Output is correct |
57 |
Correct |
956 ms |
63080 KB |
Output is correct |
58 |
Correct |
902 ms |
63080 KB |
Output is correct |
59 |
Correct |
908 ms |
63168 KB |
Output is correct |
60 |
Correct |
999 ms |
63092 KB |
Output is correct |
61 |
Correct |
912 ms |
63108 KB |
Output is correct |
62 |
Correct |
909 ms |
63172 KB |
Output is correct |
63 |
Correct |
885 ms |
63072 KB |
Output is correct |
64 |
Correct |
731 ms |
63180 KB |
Output is correct |
65 |
Correct |
754 ms |
63084 KB |
Output is correct |
66 |
Correct |
668 ms |
63304 KB |
Output is correct |
67 |
Correct |
726 ms |
63080 KB |
Output is correct |