(USACO) Greedy Gift Givers - Solution
import java.io.*;
import java.util.*;
class gift1 {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("gift1.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
int numFriends = Integer.parseInt(f.readLine());
HashMap<String, Integer> friends = new HashMap<String, Integer>();
String[] names = new String[numFriends];
for (int i = 0; i < numFriends; i++) {
String name = f.readLine();
names[i] = name;
friends.put(name, 0);
}
for (int i = 0; i < numFriends; i++) {
String name = f.readLine();
String line = f.readLine();
StringTokenizer st = new StringTokenizer(line);
int money = Integer.parseInt(st.nextToken());
friends.put(name, friends.get(name)-money);
int divisions = Integer.parseInt(st.nextToken());
if (divisions == 0) {
friends.put(name, friends.get(name)+money);
}
else {
int moneyDivided = money/divisions;
for (int j = 0; j < divisions; j++) {
String friend = f.readLine();
friends.put(friend, friends.get(friend)+moneyDivided);
}
if (money > 0) {
friends.put(name, friends.get(name)+(money-(moneyDivided*divisions)));
}
}
}
for (int i = 0; i < numFriends; i++) {
out.println(names[i] + " " + friends.get(names[i]));
}
out.close();
}
}
import java.util.*;
class gift1 {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("gift1.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
int numFriends = Integer.parseInt(f.readLine());
HashMap<String, Integer> friends = new HashMap<String, Integer>();
String[] names = new String[numFriends];
for (int i = 0; i < numFriends; i++) {
String name = f.readLine();
names[i] = name;
friends.put(name, 0);
}
for (int i = 0; i < numFriends; i++) {
String name = f.readLine();
String line = f.readLine();
StringTokenizer st = new StringTokenizer(line);
int money = Integer.parseInt(st.nextToken());
friends.put(name, friends.get(name)-money);
int divisions = Integer.parseInt(st.nextToken());
if (divisions == 0) {
friends.put(name, friends.get(name)+money);
}
else {
int moneyDivided = money/divisions;
for (int j = 0; j < divisions; j++) {
String friend = f.readLine();
friends.put(friend, friends.get(friend)+moneyDivided);
}
if (money > 0) {
friends.put(name, friends.get(name)+(money-(moneyDivided*divisions)));
}
}
}
for (int i = 0; i < numFriends; i++) {
out.println(names[i] + " " + friends.get(names[i]));
}
out.close();
}
}
Comments
Post a Comment