-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewYearParties.java
72 lines (68 loc) · 1.58 KB
/
NewYearParties.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
/*
https://codeforces.com/problemset/problem/1283/E
=> min
=> max
=> independence => seperate two .
=> two sub-problems
#greedy
*/
import java.util.Scanner;
public class NewYearParties {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int maxVal = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
// count array
int[] cntArr = new int[maxVal + 1];
for (int v : arr) {
cntArr[v]++;
}
// find min
int min = 0;
int i = 1;
while (i < maxVal + 1) {
if (cntArr[i] > 0) {
/*
i : cnt[i] > 0
|| cnt[i+1] > 0
|| cnt[i+2] > 0
=> select pos = i+1 for both i, i+1, i+2.
*/
i += 3;
min++;
} else {
i++;
}
}
// find max
int max = 0;
int prev = 0; // number of free's that can move back or forward
for (i = 1; i < maxVal + 1; i++) {
if (prev > 0) { // use prev only
max++;
prev = cntArr[i];
cntArr[i] = 1;
} else { // need to use at least one at i.
if (cntArr[i] > 0 && cntArr[i - 1] == 0) { // move left
cntArr[i - 1]++;
cntArr[i]--;
max++; // count for i - 1
}
if (cntArr[i] > 0) {
max++; // count for i
prev = cntArr[i] - 1;
}
}
}
// right bound
if (prev > 0) max++;
System.out.println(min + " " + max);
}
}