qwerty787788

DGCJ Template

Aug 5th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     static int myId = -1;
  5.  
  6.     static void err(String s) {
  7.         System.err.println("thread " + myId + ": " + s);
  8.     }
  9.  
  10.     static void out(String s) {
  11.         System.out.println(s);
  12.     }
  13.  
  14.     static void out(long s) {
  15.         System.out.println(s);
  16.     }
  17.  
  18.     final static int M = 10;
  19.  
  20.     static char get(int i, int j) {
  21.         if (j < 0 || j >= w) {
  22.             return '#';
  23.         }
  24.         if (i >= h) {
  25.             return '0';
  26.         }
  27.         return asteroids.GetPosition(i, j);
  28.     }
  29.  
  30.     static int h, w;
  31.  
  32.     public static void main(String[] args) {
  33.         // long START = System.currentTimeMillis();
  34.         myId = message.MyNodeId();
  35.         int nodes = message.NumberOfNodes();
  36.         h = (int) asteroids.GetHeight();
  37.         w = (int) asteroids.GetWidth();
  38.         int len = Math.max(M, 1 + (w - 1) / nodes);
  39.         int left = Math.min(w, len * myId);
  40.         int right = Math.min(w, len * (myId + 1));
  41.         int[] dp = new int[2 * M + (right - left)];
  42.         Arrays.fill(dp, Integer.MIN_VALUE);
  43.         char[] currentRow = new char[dp.length];
  44.         char[] nextRow = new char[dp.length];
  45.         int[] nextDp = new int[dp.length];
  46.         Arrays.fill(currentRow, '#');
  47.         for (int i = left - 1; i < right + 1; i++) {
  48.             int pos = i - left + M;
  49.             currentRow[pos] = get(0, i);
  50.             if (currentRow[pos] != '#') {
  51.                 dp[pos] = currentRow[pos] - '0';
  52.             }
  53.         }
  54.         int curLeft = left, curRight = right;
  55.         for (int i = 1; i <= h; i++) {
  56.             if (i % (M - 2) == 0) {
  57.                 if (myId != 0) {
  58.                     for (int j = 0; j < M; j++) {
  59.                         message.PutInt(myId - 1, dp[M - j - 1]);
  60.                     }
  61.                     message.Send(myId - 1);
  62.                 }
  63.                 if (myId != nodes - 1) {
  64.                     for (int j = 0; j < M; j++) {
  65.                         message.PutInt(myId + 1, dp[M + (right - left) + j]);
  66.                     }
  67.                     message.Send(myId + 1);
  68.                 }
  69.                 if (myId != 0) {
  70.                     message.Receive(myId - 1);
  71.                     for (int j = 0; j < M; j++) {
  72.                         dp[j + M] = Math.max(dp[j + M],
  73.                                 message.GetInt(myId - 1));
  74.                     }
  75.                 }
  76.                 if (myId != nodes - 1) {
  77.                     message.Receive(myId + 1);
  78.                     for (int j = 0; j < M; j++) {
  79.                         dp[M + (right - left) - j - 1] = Math.max(dp[M
  80.                                 + (right - left) - j - 1],
  81.                                 message.GetInt(myId + 1));
  82.                     }
  83.                 }
  84.                 curLeft = left;
  85.                 curRight = right;
  86.             }
  87.             Arrays.fill(nextDp, Integer.MIN_VALUE);
  88.             for (int j = curLeft - 2; j < curRight + 2; j++) {
  89.                 nextRow[j - left + M] = get(i, j);
  90.             }
  91.             for (int pos = curLeft; pos < curRight; pos++) {
  92.                 int p = pos - left + M;
  93.                 if (nextRow[p] != '#') {
  94.                     nextDp[p] = Math.max(nextDp[p], dp[p] + (nextRow[p] - '0'));
  95.                 }
  96.                 if (nextRow[p - 1] != '#' && currentRow[p - 1] != '#') {
  97.                     nextDp[p - 1] = Math.max(nextDp[p - 1], dp[p]
  98.                             + (currentRow[p - 1] - '0')
  99.                             + (nextRow[p - 1] - '0'));
  100.                 }
  101.                 if (nextRow[p + 1] != '#' && currentRow[p + 1] != '#') {
  102.                     nextDp[p + 1] = Math.max(nextDp[p + 1], dp[p]
  103.                             + (currentRow[p + 1] - '0')
  104.                             + (nextRow[p + 1] - '0'));
  105.                 }
  106.             }
  107.             curLeft--;
  108.             curRight++;
  109.             int[] tmp = dp;
  110.             dp = nextDp;
  111.             nextDp = tmp;
  112.             char[] tmp2 = currentRow;
  113.             currentRow = nextRow;
  114.             nextRow = tmp2;
  115.         }
  116.  
  117.         int result = Integer.MIN_VALUE;
  118.         for (int i = 0; i < dp.length; i++) {
  119.             result = Math.max(result, dp[i]);
  120.         }
  121.         message.PutInt(0, result);
  122.         message.Send(0);
  123.         if (myId == 0) {
  124.             for (int i = 0; i < nodes; i++) {
  125.                 message.Receive(i);
  126.                 result = Math.max(result, message.GetInt(i));
  127.             }
  128.             if (result < 0) {
  129.                 result = -1;
  130.             }
  131.             out(result);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment