import java.io.*;
import java.util.*;
public class GreedyGift {
public static void main(String[] args) throws Exception {
try {
Reader.init(System.in);
// Reader.init(new FileInputStream(new File("input.in")));
boolean isRunning = true;
boolean isFirst = true;
while (isRunning) {
int noOfPeoples = Reader.nextInt();
PeopleContainer cont = new PeopleContainer();
for (int i = 0; i < noOfPeoples; i++)
cont.addPeople(Reader.next());
for (int i = 0; i < noOfPeoples; i++) {
String gaverName = Reader.next();
int giftValues = Reader.nextInt();
int noOfGiven = Reader.nextInt();
if (noOfGiven == 0)
continue;
while (giftValues % noOfGiven != 0) {
giftValues--;
}
cont.addValue(gaverName, -giftValues);
if (noOfGiven != 0) {
int tempVal = giftValues / noOfGiven;
for (int j = 0; j < noOfGiven; j++) {
String getterName = Reader.next();
cont.addValue(getterName, tempVal);
}
}
}
if (!isFirst)
System.out.println("");
cont.printAll();
isFirst = false;
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
}
class People {
String name;
int val;
}
class PeopleContainer {
ArrayList<People> list;
PeopleContainer() {
list = new ArrayList<People>();
}
public void addPeople(String name) {
People pep = new People();
pep.name = name;
pep.val = 0;
list.add(pep);
}
public void addValue(String name, int value) {
Iterator it = list.iterator();
while (it.hasNext()) {
People tmp = (People) it.next();
if (tmp.name.equals(name)) {
tmp.val += value;
return;
}
}
}
public void printAll() {
Iterator it = list.iterator();
while (it.hasNext()) {
People tmp = (People) it.next();
System.out.println(tmp.name + " " + tmp.val);
}
}
}
/** Class for buffered reading int and double values */
/** http://www.cpe.ku.ac.th/~jim/java-io.html */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
11/08/2013
119 - Greedy Gift Givers - Java
Subscribe to:
Post Comments (Atom)
Richard Stallman, Linus Torvalds, and Donald Knuth engage in a discussion on whose impact on computer science was the greatest.
ReplyDeleteStallman: "God told me I have programmed the best editor in the world!"
Torvalds: "Well, God told me that I have programmed the best operating system in the world!"
Knuth: "Wait, wait, I never said that."