-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBestRelayTeam.java
66 lines (57 loc) · 1.96 KB
/
BestRelayTeam.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
//Name: Nguyen Minh Hieu
//https://open.kattis.com/problems/bestrelayteam
import java.util.*;
public class BestRelayTeam {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.nextLine();
HashMap<String, Double> start = new HashMap<String, Double>();
HashMap<String, Double> flying = new HashMap<String, Double>();
//input data
for (int i = 0; i < num; i ++){
String name = sc.next();
Double first = sc.nextDouble();
Double other = sc.nextDouble();
start.put(name, first);
flying.put(name, other);
}
// sort flying by values => list of names sorted by flying time
Set<Map.Entry<String, Double>> entries = flying.entrySet();
Comparator<Map.Entry<String, Double>> c = new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(Map.Entry<String, Double> e1, Map.Entry<String,Double> e2) {
Double v1 = e1.getValue();
Double v2 = e2.getValue();
return v1.compareTo(v2);
}
};
List<Map.Entry<String, Double>> list_entries = new ArrayList<Map.Entry<String, Double>>(entries);
Collections.sort(list_entries,c);
ArrayList<String> list_name = new ArrayList<String>();
for(Map.Entry<String, Double> entry : list_entries){
list_name.add(entry.getKey());
}
Double best_time = 100.0; //100 > 20 *4
ArrayList<String> best_team = null;
for (String name: list_name) {
Double temp = start.get(name);
ArrayList<String> team = new ArrayList<String>();
team.add(name);
for (String name1: list_name) {
if (name == name1) continue;
team.add(name1);
temp += flying.get(name1);
if (team.size() == 4) break;
}
if (temp < best_time) {
best_time = temp;
best_team = team;
}
}
System.out.println(best_time);
for (String name: best_team) {
System.out.println(name);
}
}
}