Esempio n. 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);
  }
Esempio n. 2
0
File: A.java Progetto: jmankhan/ACM
  public void solve() throws Exception {
    long a = in.nextLong();
    long b = in.nextLong();

    long ans = 0;
    while (a != 0 && b != 0) {
      if (a > b) {
        ans += a / b;
        a = a % b;
      } else {
        ans += b / a;
        b = b % a;
      }
    }

    out.println(ans);
  }
Esempio n. 3
0
  public void solve() throws Exception {
    String str = in.readLine();
    int n = str.length();

    int t = Integer.parseInt(in.readLine());

    int[][] dp = new int[26][n];

    dp[str.charAt(0) - 'a'][0] = 1;

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 26; j++) {
        if (str.charAt(i) == 'a' + j) {
          dp[j][i] = dp[j][i - 1] + 1;
        } else {
          dp[j][i] = dp[j][i - 1];
        }
      }
    }

    while (t-- > 0) {
      st = new StringTokenizer(in.readLine());
      char a = st.nextToken().toCharArray()[0];
      char b = st.nextToken().toCharArray()[0];

      int l = parseInt(st.nextToken()) - 1;
      int r = parseInt(st.nextToken()) - 1;

      int ans = 0;
      for (int i = l; i <= r; i++) {
        if (str.charAt(i) == a) {
          ans += dp[b - 'a'][r] - dp[b - 'a'][i];
        }
      }

      out.println(ans);
    }
  }
Esempio n. 4
0
File: C.java Progetto: jmankhan/ACM
  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");
    }
  }