-
Notifications
You must be signed in to change notification settings - Fork 0
/
Taxi.java
53 lines (46 loc) · 1.21 KB
/
Taxi.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
/* https://codeforces.com/contest/158/problem/B
#special-problem #greedy #implementation
find out: number of group of 4 people
min number of (group of 1 person, group of 3 people)
remained group of 3 people
number of cars needed to remained group of 1 person and group of 2 people.
*/
import java.util.Scanner;
public class Taxi {
static int calMinTaxiNum() {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int[] arr = new int[4];
int temp = 0;
for (int i = 0; i < number; i++) {
temp = scanner.nextInt();
arr[temp - 1] += 1;
}
// group of 4 people
int result = arr[3];
// group of 1 person and 3 people
if (arr[0] == arr[2]) {
result += arr[0];
arr[0] = 0;
arr[2] = 0;
}
if (arr[0] < arr[2]) {
result += arr[0];
arr[2] -= arr[0];
arr[0] = 0;
} else {
result += arr[2];
arr[0] -= arr[2];
arr[2] = 0;
}
result += arr[2];
// group of 1 person and 2 people
if (arr[0] != 0 || arr[1] != 0) {
result += (arr[0] + 2 * arr[1] - 1) / 4 + 1;
}
return result;
}
public static void main(String args[]) {
System.out.println(calMinTaxiNum());
}
}