#include <stdio.h>
int n;
int arr[302];
int cost[302][302][302] = {0};
void apply(int *a, int b) {
if (*a > b) {
*a = b;
}
}
int get_cost(int start, int end, int offset) {
if (end == start) {
return arr[start - 1] != (offset + 1);
}
if (cost[start][end][offset] == -1) {
int length = end - start + 1;
int min = offset + 1;
int max = length + offset;
int costtmp = 10000;
apply(&costtmp, get_cost(start + 1, end, offset + 1) + (min != arr[start- 1]));
apply(&costtmp, get_cost(start + 1, end, offset) + (max != arr[start- 1]));
apply(&costtmp, get_cost(start, end - 1, offset + 1) + (min != arr[end- 1]));
apply(&costtmp, get_cost(start, end - 1, offset) + (max != arr[end- 1]));
cost[start][end][offset] = costtmp;
// printf("%d %d %d %d %d\n", start, end, min, max, costtmp);
}
return cost[start][end][offset];
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n + 1; j++) {
for (int k = 0; k < n + 1; k++) {
cost[i][j][k] = -1;
}
}
}
printf("%d", get_cost(1, n, 0));
return 0;
}
Compilation message
giraffes.c: In function 'main':
giraffes.c:39:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
giraffes.c:41:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d", &arr[i]);
| ^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
608 KB |
Output is correct |
14 |
Correct |
0 ms |
424 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
0 ms |
684 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
608 KB |
Output is correct |
14 |
Correct |
0 ms |
424 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
0 ms |
684 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
11 ms |
11868 KB |
Output is correct |
23 |
Correct |
66 ms |
41816 KB |
Output is correct |
24 |
Correct |
132 ms |
70932 KB |
Output is correct |
25 |
Correct |
233 ms |
100772 KB |
Output is correct |
26 |
Correct |
230 ms |
105812 KB |
Output is correct |
27 |
Correct |
235 ms |
106480 KB |
Output is correct |
28 |
Correct |
230 ms |
107088 KB |
Output is correct |
29 |
Correct |
252 ms |
107508 KB |
Output is correct |
30 |
Correct |
245 ms |
107868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
608 KB |
Output is correct |
14 |
Correct |
0 ms |
424 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
0 ms |
684 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
11 ms |
11868 KB |
Output is correct |
23 |
Correct |
66 ms |
41816 KB |
Output is correct |
24 |
Correct |
132 ms |
70932 KB |
Output is correct |
25 |
Correct |
233 ms |
100772 KB |
Output is correct |
26 |
Correct |
230 ms |
105812 KB |
Output is correct |
27 |
Correct |
235 ms |
106480 KB |
Output is correct |
28 |
Correct |
230 ms |
107088 KB |
Output is correct |
29 |
Correct |
252 ms |
107508 KB |
Output is correct |
30 |
Correct |
245 ms |
107868 KB |
Output is correct |
31 |
Runtime error |
182 ms |
218964 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |