#include <stdio.h>
#define N 100000
#define N_ 5000
int max(int a, int b) { return a > b ? a : b; }
int main() {
static int aa[N], bb[N], qu[N], pp[N], qq[N], dp[N_][N_];
int n, cnt, same, i, j, b, k, good;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
scanf("%d", &bb[i]);
same = 1;
for (i = 1; i < n; i++)
if (bb[i] != bb[i - 1]) {
same = 0;
break;
}
if (same) {
b = bb[0], k = 0;
for (i = 0; i < n; i++)
if (aa[i] <= b) {
j = i;
good = 0;
while (j < n && aa[j] <= b)
if (aa[j++] == b)
good = 1;
if (good)
k += j - i;
i = j;
}
printf("%d\n", k);
return 0;
}
cnt = 0;
for (i = 0; i < n; i++) {
while (cnt && aa[qu[cnt - 1]] <= aa[i])
cnt--;
pp[i] = cnt ? qu[cnt - 1] : -1;
qu[cnt++] = i;
}
cnt = 0;
for (i = n - 1; i >= 0; i--) {
while (cnt && aa[qu[cnt - 1]] <= aa[i])
cnt--;
qq[i] = cnt ? qu[cnt - 1] : n;
qu[cnt++] = i;
}
if (n <= N_) {
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
dp[i][j] = max(i == 0 ? 0 : dp[i - 1][j], (j == 0 ? 0 : dp[i][j - 1]) + (aa[i] == bb[j] && pp[i] < j && j < qq[i] ? 1 : 0));
printf("%d\n", dp[n - 1][n - 1]);
}
return 0;
}
Compilation message
exam.c: In function 'main':
exam.c:12:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
exam.c:14:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
exam.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d", &bb[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
336 KB |
Output is correct |
3 |
Correct |
18 ms |
968 KB |
Output is correct |
4 |
Correct |
17 ms |
1044 KB |
Output is correct |
5 |
Correct |
26 ms |
1008 KB |
Output is correct |
6 |
Correct |
16 ms |
976 KB |
Output is correct |
7 |
Correct |
18 ms |
1004 KB |
Output is correct |
8 |
Correct |
25 ms |
1180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
16 ms |
24020 KB |
Output is correct |
4 |
Correct |
60 ms |
94396 KB |
Output is correct |
5 |
Correct |
65 ms |
98252 KB |
Output is correct |
6 |
Correct |
66 ms |
98320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
1192 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
3284 KB |
Output is correct |
9 |
Correct |
16 ms |
24020 KB |
Output is correct |
10 |
Correct |
60 ms |
94396 KB |
Output is correct |
11 |
Correct |
65 ms |
98252 KB |
Output is correct |
12 |
Correct |
66 ms |
98320 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
1192 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
5 ms |
8180 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
110 ms |
98240 KB |
Output is correct |
25 |
Correct |
73 ms |
98220 KB |
Output is correct |
26 |
Correct |
68 ms |
98252 KB |
Output is correct |
27 |
Correct |
68 ms |
98224 KB |
Output is correct |
28 |
Correct |
71 ms |
98252 KB |
Output is correct |
29 |
Correct |
69 ms |
98300 KB |
Output is correct |
30 |
Correct |
65 ms |
98288 KB |
Output is correct |
31 |
Correct |
66 ms |
98220 KB |
Output is correct |
32 |
Correct |
67 ms |
98220 KB |
Output is correct |