Pages

Friday, December 14, 2007

USACO: Greedy Gift Givers

/*
ID: wsoethe1
LANG: JAVA
TASK: gift1
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class gift1 {

public static void main(String[] args) {
try {
BufferedReader fileIn = new BufferedReader(new FileReader(
"gift1.in"));
PrintWriter fileOut = new PrintWriter(new BufferedWriter(
new FileWriter("gift1.out")));
try {
int quantity = Integer.parseInt(fileIn.readLine());
String[][] outFile = new String[quantity][2];
for (int i = 0; i < quantity; i++) {
outFile[i][0] = fileIn.readLine();
outFile[i][1] = "0";
}
for (int i = 1; i <= quantity; i++) {
String giver = fileIn.readLine();
String temp = fileIn.readLine();
int money = Integer.parseInt(temp.split(" ")[0]);
int receivers = Integer.parseInt(temp.split(" ")[1]);
int giversValue = 0;
if (money != 0 || receivers != 0) {
giversValue = (money % receivers) - money;
}
for (int j = 0; j < outFile.length; j++) {
if (outFile[j][0].equals(giver)) {
outFile[j][1] = new Integer(giversValue
+ Integer.parseInt(outFile[j][1]))
.toString();
break;
}
}
if (receivers != 0) {
money = (money - (money % receivers)) / receivers;
for (int j = 1; j <= receivers; j++) {
temp = fileIn.readLine();
for (int k = 0; k < outFile.length; k++) {
if (outFile[k][0].equals(temp)) {
outFile[k][1] = new Integer(money
+ Integer.parseInt(outFile[k][1]))
.toString();
break;
}
}

}
}
}
for (int i = 0; i < outFile.length; i++) {
fileOut.write(outFile[i][0] + " " + outFile[i][1] + "\n");
}
} finally {
fileIn.close();
fileOut.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

No comments :