-
Notifications
You must be signed in to change notification settings - Fork 0
/
Garland.java
76 lines (73 loc) · 1.86 KB
/
Garland.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* https://codeforces.com/contest/1287/problem/C #dynamic-programming #greedy #top-down #bottom-up
* #todo
*/
import java.util.Scanner;
class Garland {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n + 1];
int oddCnt = 0;
int evenCnt = 0;
for (int i = 1; i < n + 1; i++) {
arr[i] = sc.nextInt();
if (arr[i] != 0) {
if (arr[i] % 2 == 0) {
evenCnt += 1;
} else {
oddCnt += 1;
}
}
}
// count missed numbers
evenCnt = n / 2 - evenCnt;
oddCnt = n / 2 - oddCnt;
if (n % 2 != 0) {
oddCnt += 1;
}
int[][] L = new int[n + 1][2];
// L[i][even][odd][m] min complexity length i, remain even, odd, modulo m
// L[n][0][0][0] /
for (int i = 1; i <= n; i++) {
if (arr[i] != 0) {
if (i != 1) {
if ((arr[i] - arr[i - 1]) % 2 != 0) {
L[i][arr[i] % 2] = L[i][arr[i - 1] % 2] + 1;
L[i][(arr[i] + 1) % 2] = L[i][(arr[i - 1] + 1) % 2];
} else {
L[i][0] = L[i - 1][0];
L[i][1] = L[i - 1][1];
}
}
continue;
}
// add removed elements
if (arr[i - 1] % 2 != 0) { // odd
if (oddCnt > 0) {
oddCnt--;
arr[i] = 1;
L[i][0] = L[i - 1][0];
L[i][1] = L[i - 1][1];
} else {
evenCnt--;
L[i][0] = L[i - 1][0] + 1;
L[i][1] = L[i - 1][1];
}
} else { // even
if (evenCnt > 0) {
evenCnt--;
L[i][0] = L[i - 1][0];
L[i][1] = L[i - 1][1];
} else {
oddCnt--;
L[i][0] = L[i - 1][0];
L[i][1] = L[i - 1][1] + 1;
}
}
}
for (int i = 0; i <= n; i++) {
System.out.println(L[i][0] + ", " + L[i][1]);
}
}
}