Guest User

a

a guest
Feb 6th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. // div2 easy
  2. public class CoinFlipsDiv2 {
  3.   public int countCoins(String state) {
  4.     int n = state.length();
  5.     int res = 0;
  6.     for (int i = 0; i < n; i++) {
  7.       boolean add = false;
  8.       if (i > 0 && state.charAt(i-1) != state.charAt(i)) add=true;
  9.       if (i+1 < n && state.charAt(i+1) != state.charAt(i)) add=true;
  10.       if (add) res++;
  11.     }
  12.     return res;
  13.   }
  14. }
  15.  
  16. // div2 med
  17. public class ExplodingRobots {
  18.   public String canExplode(int x1, int y1, int x2, int y2, String instructions) {
  19.     int x = Math.abs(x1-x2);
  20.     int y = Math.abs(y1-y2);
  21.     for (char c : instructions.toCharArray()) {
  22.       if (c == 'R' || c == 'L') x -= 1;
  23.       if (c == 'U' || c == 'D') y -= 1;
  24.     }
  25.     return x <= 0 && y <= 0 ? "Explosion" : "Safe";
  26.   }
  27. }
  28.  
  29. // div2 hard
  30. import java.util.Arrays;
  31. public class XorLists {
  32.   public int n, m;
  33.   public int[][] b;
  34.   public int countLists(int[] s, int max) {
  35.     n = 0;
  36.     m = max;
  37.     while(n*n < s.length) n++;
  38.    
  39.     b = new int[n][n];
  40.     for (int i = 0; i < n; i++) {
  41.       for (int j = 0; j < n; j++) {
  42.         b[i][j] = s[i*n+j];
  43.       }
  44.     }
  45.    
  46.     for (int i = 0; i < n; i++) {
  47.       for (int j = 0; j < n; j++) {
  48.         for (int k = 0; k < n; k++) {
  49.           if ((b[i][j] ^ b[j][k]) != b[i][k]) {
  50.             return 0;
  51.           }
  52.         }
  53.       }
  54.     }
  55.    
  56.     dp = new int[32][(1<<n)];
  57.     for(int[] x : dp) Arrays.fill(x, -1);
  58.     return count(31, 0);
  59.   }
  60.   public int[][] dp;
  61.   public int count(int bit, int mask) {
  62.     if (bit < 0) return 1;
  63.     if (dp[bit][mask] != -1) return dp[bit][mask];
  64.    
  65.     int[] a = new int[n];
  66.     for (int i = 1; i < n; i++) {
  67.       a[i] = (b[0][i] >> bit) & 1;
  68.     }
  69.     int count = 0;
  70.     int cb = (m >> bit) & 1;
  71.     for (int tt = 0; tt < 2; tt++) {
  72.       int nmask = mask;
  73.       boolean ok = true;
  74.       for (int i = 0; i < n; i++) {
  75.         if (a[i] > cb && (mask&(1<<i)) == 0) {
  76.           ok = false;
  77.         }
  78.         if (a[i] < cb) nmask |= 1 << i;
  79.       }
  80.       if (ok) {
  81.         count += count(bit-1, nmask);
  82.       }
  83.       for (int i = 0; i < n; i++) a[i] ^= 1;
  84.     }
  85.     return dp[bit][mask] = count;
  86.   }
  87. }
Add Comment
Please, Sign In to add comment