UVA 119 – Greedy Gift Givers

Pretty simple.

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P119 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    boolean first = true;
    while (sc.hasNextInt()) {
      if (!first)
        out.println();
      int N = sc.nextInt();
      String[] friends = new String[N];
      HashMap<String, Integer> map = new HashMap<String, Integer>();
      for (int i = 0; i < N; i++) {
        friends[i] = sc.next();
        map.put(friends[i], i);
      }
      int[] moneys = new int[N];
      for (int i = 0; i < N; i++) {
        int idx = map.get(sc.next());
        int gift = sc.nextInt();
        int M = sc.nextInt();
        if (M > 0) {
          moneys[idx] -= gift;
          if (gift != 0 && gift % M != 0) {
            int t = gift % M;
            moneys[idx] += t;
            gift -= t;
          }
          for (int j = 0; j < M; j++) {
            moneys[map.get(sc.next())] += gift / M;
          }
        }
      }
      for (int i = 0; i < N; i++) {
        out.println(friends[i] + " " + moneys[i]);
      }
      first = false;
    }
    out.close();
    sc.close();
  }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.