qwerty787788

DGCJ

May 29th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.Map.Entry;
  5. import java.util.Random;
  6.  
  7. public class Main {
  8.     static int myId = -1;
  9.  
  10.     static void err(String s) {
  11.         System.err.println("thread " + myId + ": " + s);
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         // long START = System.currentTimeMillis();
  16.         myId = message.MyNodeId();
  17.         int nodes = message.NumberOfNodes();
  18.         int n = (int) winning_move.GetNumPlayers();
  19.         int len = 1 + (n - 1) / nodes;
  20.         int left = Math.min(n, len * myId);
  21.         int right = Math.min(n, len * (myId + 1));
  22.         final int MOD = 100003;
  23.         Random rnd = new Random(123);
  24.         int[] id = new int[MOD];
  25.         for (int i = 0; i < MOD; i++) {
  26.             id[i] = rnd.nextInt(nodes);
  27.         }
  28.         int sz = right - left;
  29.         long[] h = new long[sz];
  30.         for (int i = 0; i < sz; i++) {
  31.             h[i] = winning_move.GetSubmission(i + left);
  32.         }
  33.         HashMap<Long, Integer> cnt = new HashMap<>();
  34.         for (long v : h) {
  35.             Integer count = cnt.get(v);
  36.             if (count == null) {
  37.                 count = 0;
  38.             }
  39.             cnt.put(v, count + 1);
  40.         }
  41.         ArrayList<Value> all = new ArrayList<>();
  42.         for (Entry<Long, Integer> entry : cnt.entrySet()) {
  43.             all.add(new Value(entry.getKey(), entry.getValue(), id[(int)(entry.getKey() % MOD)]));
  44.         }
  45.         Collections.sort(all);
  46.         int it = 0;
  47.         for (int i = 0; i < nodes; i++) {
  48.             ArrayList<Value> now = new ArrayList<>();
  49.             while (it < all.size() && all.get(it).sendTo == i) {
  50.                 now.add(all.get(it));
  51.                 it++;
  52.             }
  53.             message.PutInt(i, now.size());
  54.             for (int j = 0; j < now.size(); j++) {
  55.                 message.PutLL(i, now.get(j).value);
  56.                 message.PutInt(i, now.get(j).cnt);
  57.             }
  58.             message.Send(i);
  59.         }
  60.         cnt = new HashMap<>();
  61.         for (int i = 0; i < nodes; i++) {
  62.             message.Receive(i);
  63.             int s = message.GetInt(i);
  64.             for (int j = 0; j < s; j++) {
  65.                 long value = message.GetLL(i);
  66.                 int cntValue = message.GetInt(i);
  67.                 Integer count = cnt.get(value);
  68.                 if (count == null) {
  69.                     count = 0;
  70.                 }
  71.                 cnt.put(value, count + cntValue);
  72.             }
  73.         }
  74.         Long best = Long.MAX_VALUE;
  75.         for (Entry<Long, Integer> entry : cnt.entrySet()) {
  76.             if (entry.getKey() < best && entry.getValue() == 1) {
  77.                 best = entry.getKey();
  78.             }
  79.         }
  80.         message.PutLL(0, best);
  81.         message.Send(0);
  82.         if (myId == 0) {
  83.             for (int i = 0; i < nodes; i++) {
  84.                 message.Receive(i);
  85.                 best = Math.min(best, message.GetLL(i));
  86.             }
  87.             System.out.println(best == Long.MAX_VALUE ? 0 : best);
  88.         }
  89.     }
  90.  
  91.     static class Value implements Comparable<Value> {
  92.         long value;
  93.         int cnt;
  94.         int sendTo;
  95.  
  96.         public Value(long value, int cnt, int sendTo) {
  97.             super();
  98.             this.value = value;
  99.             this.cnt = cnt;
  100.             this.sendTo = sendTo;
  101.         }
  102.  
  103.         @Override
  104.         public int compareTo(Value arg0) {
  105.             return Integer.compare(sendTo, arg0.sendTo);
  106.         }
  107.  
  108.     }
  109. }
Add Comment
Please, Sign In to add comment