Skip to content

Instantly share code, notes, and snippets.

@Dhruval10
Forked from stphung/gift1.java
Created September 18, 2016 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dhruval10/baad3fae5ad6a9860a5a78c8f8d0e900 to your computer and use it in GitHub Desktop.
Save Dhruval10/baad3fae5ad6a9860a5a78c8f8d0e900 to your computer and use it in GitHub Desktop.
My solution to "Greedy Gift Givers" from USACO
/*
ID: stphung1
LANG: JAVA
TASK: gift1
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class gift1 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("gift1.in"));
PrintWriter pw = new PrintWriter(new File("gift1.out"));
int np = sc.nextInt();
List<String> people = new ArrayList<String>();
for (int i = 0; i < np; i++) {
people.add(sc.next());
}
Map<String, Integer> received = new HashMap<String, Integer>();
for (String person : people) {
received.put(person, 0);
}
Map<String, Integer> initial = new HashMap<String, Integer>();
for (int i = 0; i < np; i++) {
String person = sc.next();
int amount = sc.nextInt();
int recipients = sc.nextInt();
initial.put(person, amount);
int giftAmount = 0;
if (recipients > 0) {
giftAmount = amount / recipients;
received.put(person, received.get(person) + amount % recipients);
}
for (int j = 0; j < recipients; j++) {
String recipient = sc.next();
received.put(recipient, received.get(recipient) + giftAmount);
}
}
for (String person : people) {
pw.println(person + " " + (received.get(person) - initial.get(person)));
}
pw.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment