git reimport
This commit is contained in:
100
archive/2017.09/2017.09.19 - unsorted/K.java
Normal file
100
archive/2017.09/2017.09.19 - unsorted/K.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package chelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
import misc.Couple;
|
||||
import misc.Pair;
|
||||
import misc.SimpleSavingChelperSolution;
|
||||
|
||||
|
||||
public class K extends SimpleSavingChelperSolution {
|
||||
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
wrapSolve(testNumber, in, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void solve(int testNumber) {
|
||||
int n = in.nextInt();
|
||||
int a = in.nextInt();
|
||||
int b = in.nextInt();
|
||||
int k = in.nextInt();
|
||||
int f = in.nextInt();
|
||||
|
||||
Map<String, Integer> nameToId = new HashMap<>();
|
||||
|
||||
List<Couple<Integer>> trips = new ArrayList<>();
|
||||
|
||||
Map<Couple<Integer>, Integer> costs = new HashMap<>();
|
||||
|
||||
int prev = -1;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
String s1 = in.nextString();
|
||||
String s2 = in.nextString();
|
||||
|
||||
if (!nameToId.containsKey(s1)) {
|
||||
nameToId.put(s1, nameToId.size());
|
||||
}
|
||||
if (!nameToId.containsKey(s2)) {
|
||||
nameToId.put(s2, nameToId.size());
|
||||
}
|
||||
|
||||
int id1 = nameToId.get(s1);
|
||||
int id2 = nameToId.get(s2);
|
||||
|
||||
Couple<Integer> couple = new Couple<>(id1, id2);
|
||||
trips.add(couple);
|
||||
|
||||
int cost = a;
|
||||
|
||||
if (prev == id1) {
|
||||
cost = b;
|
||||
}
|
||||
prev = id2;
|
||||
|
||||
if (id1 > id2) {
|
||||
int t = id1;
|
||||
id1 = id2;
|
||||
id2 = t;
|
||||
}
|
||||
couple = new Couple<>(id1, id2);
|
||||
|
||||
costs.putIfAbsent(couple, 0);
|
||||
costs.put(couple, costs.get(couple) + cost);
|
||||
}
|
||||
|
||||
List<Pair<Couple<Integer>, Integer>> coupleCosts = new ArrayList<>();
|
||||
|
||||
int total = 0;
|
||||
|
||||
for (Map.Entry<Couple<Integer>, Integer> entry : costs.entrySet()) {
|
||||
coupleCosts.add(new Pair<>(entry.getKey(), entry.getValue()));
|
||||
total += entry.getValue();
|
||||
}
|
||||
|
||||
coupleCosts.sort(new Comparator<Pair<Couple<Integer>, Integer>>() {
|
||||
@Override
|
||||
public int compare(Pair<Couple<Integer>, Integer> o1, Pair<Couple<Integer>, Integer> o2) {
|
||||
return -Integer.compare(o1.second, o2.second);
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < Math.min(k, coupleCosts.size()); i++) {
|
||||
int orig = coupleCosts.get(i).second;
|
||||
if (orig > f) {
|
||||
total += f - orig;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
out.println(total);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user