Advertisement
Guest User

aa

a guest
Dec 18th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. // div2 easy
  2. public class FarmvilleDiv2 {
  3.     public int minTime(int[] time, int[] cost, int budget) {
  4.         int n = time.length;
  5.         for (int i = 0; i < n; i++) {
  6.             for (int j = i+1; j < n; j++) {
  7.                 if (cost[i] > cost[j]) {
  8.                     int t;
  9.                     t = cost[i]; cost[i] = cost[j]; cost[j] = t;
  10.                     t = time[i]; time[i] = time[j]; time[j] = t;
  11.                 }
  12.             }
  13.         }
  14.         int sum = 0;
  15.         for (int i = 0; i < n; i++) {
  16.             sum += time[i];
  17.             int take = Math.min(time[i], budget / cost[i]);
  18.             budget -= cost[i] * take;
  19.             sum -= take;
  20.         }
  21.         return sum;
  22.     }
  23. }
  24.  
  25. // div2 med
  26. public class BoardEscapeDiv2 {
  27.     public int[] dx = {-1,0,1,0}, dy = {0,-1,0,1};
  28.     public String findWinner(String[] s, int k) {
  29.         int n = s.length, m = s[0].length();
  30.         char[][] board = new char[n][m];
  31.         for (int i = 0; i < n; i++)
  32.             board[i] = s[i].toCharArray();
  33.        
  34.         boolean[][] win = new boolean[n][m];
  35.         for (int r = 0; r < k; r++) {
  36.             boolean[][] nwin = new boolean[n][m];
  37.             for (int i = 0; i < n; i++) {
  38.                 for (int j = 0; j < m; j++) {
  39.                     if (board[i][j] == 'E') continue;
  40.                     for (int w = 0; w < 4; w++) {
  41.                         int ni = i+dx[w], nj = j+dy[w];
  42.                         if (ni >= 0 && ni < n && nj >= 0 && nj < m && board[ni][nj] != '#') {
  43.                             nwin[i][j] |= !win[ni][nj];
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.             win = nwin;
  49.         }
  50.        
  51.         int tx = 0, ty = 0;
  52.         outer : for (int i = 0; i < n; i++) {
  53.             for (int j = 0; j < m; j++) {
  54.                 if (board[i][j] == 'T') {
  55.                     tx = i;
  56.                     ty = j;
  57.                     break outer;
  58.                 }
  59.             }
  60.         }
  61.        
  62.         return win[tx][ty] ? "Alice" : "Bob";
  63.        
  64.     }
  65. }
  66.  
  67.  
  68. // div2 hard
  69. import java.util.ArrayList;
  70. public class RailroadSwitchOperator {
  71.   public int minEnergy(int N, int[] x, int[] t) {
  72.     int m = t.length;
  73.     its = new ArrayList<>();
  74.     curstate = new int[4*(N+1)];
  75.     lasttime = new int[4*(N+1)];
  76.     for (int i = 0; i < m; i++) {
  77.       simulate(t[i], 1, 1, N, x[i]);
  78.     }
  79.    
  80.     int ans = 0;
  81.     while (its.size() > 0) {
  82.       ++ans;
  83.       int min = Integer.MAX_VALUE;
  84.       for (Interval w : its) {
  85.         if (w.b < min) {
  86.           min = w.b;
  87.         }
  88.       }
  89.      
  90.       ArrayList<Interval> next = new ArrayList<>();
  91.       for (Interval w : its) {
  92.         if (min < w.a)
  93.           next.add(w);
  94.       }
  95.      
  96.       its = next;
  97.     }
  98.     return ans;
  99.   }
  100.  
  101.   static class Interval {
  102.     public int a,b;
  103.     public Interval(int a, int b) {
  104.       this.a = a;
  105.       this.b = b;
  106.     }
  107.   }
  108.   public ArrayList<Interval> its;
  109.   public int[] curstate;
  110.   public int[] lasttime;
  111.   public void simulate(int curtime, int curindex, int start, int end, int pos) {
  112.     if (start == pos && end == pos) return;
  113.     int mid = (start+end) / 2;
  114.     int need = mid >= pos ? 0 : 1;
  115.     if (curstate[curindex] != need) {
  116.       its.add(new Interval(lasttime[curindex]+1, curtime));
  117.       curstate[curindex] = need;
  118.     }
  119.     lasttime[curindex] = curtime;
  120.     if (mid >= pos) simulate(curtime+1, 2*curindex, start, mid, pos);
  121.     else simulate(curtime+1, 2*curindex+1, mid+1, end, pos);
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement