Beispiel #1
0
  public void solve() throws Exception {
    int n = in.nextInt();
    int[] a = new int[n];
    int sum = 0;

    for (int i = 0; i < n; i++) {
      a[i] = in.nextInt();
      sum += a[i];
    }

    sum /= 2;

    boolean[] dp = new boolean[sum + 1];
    dp[0] = true;

    for (int tmp : a) {
      for (int i = sum; i >= tmp; i--) {
        if (dp[i - tmp]) {
          dp[i] = true;
        }
      }
    }

    String ans = dp[sum] ? "YES" : "NO";
    out.println(ans);
  }
Beispiel #2
0
  void run() {
    Scanner sc = new Scanner(System.in);
    int oo = sc.nextInt();
    for (int o = 1; o <= oo; o++) {
      que = new PriorityQueue<E>();
      int n = sc.nextInt();
      tank = new Tank();
      count = 0;
      int[] h = new int[n + 3], b = new int[n + 3];
      h[0] = 100;
      b[0] = 0;
      h[n + 1] = 50;
      b[n + 1] = 100;
      h[n + 2] = 50;
      b[n + 2] = INF;
      for (int i = 1; i <= n; i++) {
        b[i] = sc.nextInt();
        h[i] = sc.nextInt();
      }
      n += 3;
      for (int i = 1; i < n; i++) {
        tank.bs.add(new Box(b[i - 1], b[i], h[i - 1], h[i], 0, 0));
      }
      int m = sc.nextInt();
      for (int i = 0; i < m; i++) {
        int f = sc.nextInt();
        double a = sc.nextDouble() / 30;
        for (Box box : tank.bs) {
          if (box.b1 < f && f < box.b2) box.f += a;
        }
      }
      debug(tank.bs);

      int l = sc.nextInt();
      res = new double[l];
      for (int i = 0; i < l; i++) {
        que.offer(new W(sc.nextInt(), sc.nextDouble(), i));
      }
      que.offer(tank.nextEvent());
      debug(que.size());
      while (!que.isEmpty()) {
        if (res[0] < 0) System.exit(1);
        que.poll().go();
        debug(que.peek().time());
        debug(tank.bs);
        debug(res);
        debug();
        if (count == l) break;
      }
      for (double r : res) {
        System.out.println(r);
      }
    }
  }
Beispiel #3
0
 public void run() {
   // Scanner sc = new Scanner(System.in);
   Scanner sc = new Scanner();
   StringBuilder sb = new StringBuilder(1000000);
   // System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
   final int N = sc.nextInt();
   final int M = sc.nextInt();
   final int K = (int) floor(sqrt(N));
   final int[] A = new int[N + 1];
   A[0] = 1;
   for (int i = 1; i <= N; i++) A[i] = sc.nextInt();
   final int[] next = new int[N + 1];
   final int[] step = new int[N + 1];
   final int[] last = new int[N + 1];
   for (int i = N; i > 0; i--) {
     int j = i + A[i];
     if (j > N || j / K > i / K) {
       last[i] = i;
       step[i] = 1;
       next[i] = j;
     } else {
       last[i] = last[j];
       step[i] = step[j] + 1;
       next[i] = next[j];
     }
   }
   for (int t = 0; t < M; t++)
     if (sc.nextInt() == 1) {
       int i = sc.nextInt();
       int j = 0;
       int k = 0;
       while (i <= N) {
         j += step[i];
         k = last[i];
         i = next[i];
       }
       sb.append(k).append(' ').append(j).append('\n');
       // System.out.println(k + " " + j);
     } else {
       int k = sc.nextInt();
       int b = k / K * K;
       A[k] = sc.nextInt();
       for (int i = min(b + K - 1, N); i >= b; i--) {
         int j = i + A[i];
         if (j > N || j / K > i / K) {
           last[i] = i;
           step[i] = 1;
           next[i] = j;
         } else {
           last[i] = last[j];
           step[i] = step[j] + 1;
           next[i] = next[j];
         }
       }
     }
   // System.out.flush();
   System.out.print(sb);
 }
Beispiel #4
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    for (int N = sc.nextInt(); N > 0; N = sc.nextInt()) {
      int M = sc.nextInt();
      int L = sc.nextInt();
      boolean[][] map = new boolean[N][N];
      int[][] D = new int[N][N];
      int[][] E = new int[N][N];
      while (M-- > 0) {
        int A = sc.nextInt() - 1;
        int B = sc.nextInt() - 1;
        map[A][B] = map[B][A] = true;
        D[A][B] = D[B][A] = sc.nextInt();
        E[A][B] = E[B][A] = sc.nextInt();
      }

      boolean[][] visited = new boolean[N][L + 1];
      Queue<Edge> q = new PriorityQueue<Edge>();
      q.offer(new Edge(0, 0, L));
      while (!q.isEmpty()) {
        Edge e = q.poll();
        if (visited[e.to][e.money]) continue;
        visited[e.to][e.money] = true;
        if (e.to == N - 1) {
          System.out.println(e.enemy);
          break;
        }

        for (int i = 0; i < N; i++)
          if (map[e.to][i]) {
            if (D[e.to][i] <= e.money) q.offer(new Edge(e.enemy, i, e.money - D[e.to][i]));
            q.offer(new Edge(e.enemy + E[e.to][i], i, e.money));
          }
      }
    }
  }
Beispiel #5
0
    protected String solve(Scanner sc) {
      int N = sc.nextInt();
      int K = sc.nextInt();
      char[][] board = new char[N][];
      for (int i = 0; i < N; i++) board[i] = sc.next().toCharArray();
      char[][] rot = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) rot[i][j] = board[N - j - 1][i];
      char[][] grav = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) grav[i][j] = '.';
      for (int i = 0; i < N; i++)
        for (int j = N - 1, c = 0; j >= 0; j--)
          if (rot[j][i] == '.') c++;
          else grav[j + c][i] = rot[j][i];
      //			for (char[] cs : grav)
      //				debug(new String(cs));
      int[][][] R = new int[N][N][4];
      int[][][] B = new int[N][N][4];
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          switch (grav[i][j]) {
            case 'B':
              {
                for (int d = 0; d < 4; d++)
                  B[i][j][d] = (in(i + di[d], j + dj[d], N) ? B[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
            case 'R':
              {
                for (int d = 0; d < 4; d++)
                  R[i][j][d] = (in(i + di[d], j + dj[d], N) ? R[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
          }
      boolean Rwin = false, Bwin = false;
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          for (int d = 0; d < 4; d++) {
            if (R[i][j][d] >= K) Rwin = true;
            if (B[i][j][d] >= K) Bwin = true;
          }

      return Rwin ? Bwin ? "Both" : "Red" : Bwin ? "Blue" : "Neither";
    }
Beispiel #6
0
  public void solve() throws Exception {
    n = in.nextInt();
    m = in.nextInt();

    graph = new char[n][m];
    cracked = new boolean[n][m];

    for (int i = 0; i < n; i++) {
      graph[i] = in.next().trim().toCharArray();
    }
    sx = in.nextInt() - 1;
    sy = in.nextInt() - 1;

    gx = in.nextInt() - 1;
    gy = in.nextInt() - 1;

    cracked[sx][sy] = true;

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {}
    }

    int scount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        scount++;
      }
    }

    int gcount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = gx + dx[i];
      int ny = gy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        gcount++;
      }
    }

    boolean isNeighbors = false;

    for (int i = 0; i < 4 && !isNeighbors; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }
      if (nx == gx && ny == gy) {
        isNeighbors = true;
      }
    }

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {
        if (scount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      } else {
        if (scount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (isNeighbors) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (dfs(sx, sy)) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else {
      out.println("NO");
    }
  }
Beispiel #7
0
 public void run() {
   Scanner sc = new Scanner(System.in);
   int T = sc.nextInt();
   sc.nextLine();
   for (int t = 1; t <= T; t++) System.out.printf("Case #%s: %s%n", t, solve(sc));
 }