public static void main(String[] args) {
    BufferedReader in;

    try {
      in =
          new BufferedReader(
              new FileReader(
                  "/Users/rahulkhairwar/Documents/IntelliJ IDEA Workspace/Competitive "
                      + "Programming/src/com/google/codejam16/qualificationround/inputB.txt"));

      OutputWriter out = new OutputWriter(System.out);
      Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 28);

      thread.start();

      try {
        thread.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      out.flush();

      in.close();
      out.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      String a = in.nextString();
      String b = in.nextString();
      int[][] dp = new int[2][b.length() + 1];
      dp[1][0] = 1;
      for (int i = 0; i <= b.length(); i++) {
        dp[0][i] = i;
      }

      for (int i = 1, r = a.length(); i <= r; i++) {
        dp[i % 2][0] = i;
        for (int j = 1, c = b.length(); j <= c; j++) {
          if (a.charAt(i - 1) == b.charAt(j - 1)) {
            dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
          } else {
            dp[i % 2][j] = 1 + min(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1], dp[i % 2][j - 1]);
          }
        }
      }

      out.println(dp[a.length() % 2][b.length()]);
    }

    out.flush();
    out.close();
  }
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    Solver solver = new Solver(in, out);

    solver.solve();
    in.close();
    out.flush();
    out.close();
  }
  public static void main(String[] args) {
    in = new InputReader(System.in);
    out = new OutputWriter(System.out);

    solve();

    out.flush();

    in.close();
    out.close();
  }
Пример #5
0
    public static void main(String[] argv) throws IOException
    {
        Task t;
        boolean home = argv.length > 0 && argv[0].equals("test");
        InputStream inputStream;
        OutputStream outputStream;
        if (home) {
            inputStream = new FileInputStream(Task.filename + ".in");
            outputStream = new FileOutputStream(Task.filename + ".out");
        } else {
            switch (Task.read) {
                case 0:
                    inputStream = new FileInputStream(Task.filename + ".in");
                    outputStream = new FileOutputStream(Task.filename + ".out");
                    break;
                case 1:
                    inputStream = new FileInputStream("input.txt");
                    outputStream = new FileOutputStream("output.txt");
                    break;
                default:
                    inputStream = System.in;
                    outputStream = System.out;
                    break;

            }

        }
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream, home);


        if (home)
            do {
                long time = System.currentTimeMillis();
                t = new Task();
                t.in = in;
                t.out = out;
                t.run();

                out.writeln();
                out.writeln("=====Time:" + (System.currentTimeMillis() - time));
                out.flush();
            } while (in.toNextTest());
        else {
            t = new Task();
            t.in = in;
            t.out = out;
            t.run();
        }

        out.close();

    }
Пример #6
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      int n = in.nextInt();
      out.println(countTri(n));
    }

    out.flush();
    out.close();
  }
Пример #7
0
 public static void main(String[] args) throws IOException {
   // InputStream inputStream = new FileInputStream("replaceme.in");
   // OutputStream outputStream = new FileOutputStream("replaceme.out");
   /////////////////////////////////////////////////////////////////////
   InputStream inputStream = System.in;
   OutputStream outputStream = System.out;
   InputReader in = new InputReader(inputStream);
   OutputWriter out = new OutputWriter(outputStream);
   Task solver = new Task();
   solver.solve(1, in, out);
   out.close();
   System.exit(0);
 }
Пример #8
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    while (true) {
      int n = in.nextInt();
      if (n == 0) break;
      int c = 0;
      while (n > 1) {
        n /= 2;
        c++;
      }

      out.println(c);
    }
    out.flush();
    out.close();
  }
Пример #9
0
 public void solve(int testNumber, InputReader in, OutputWriter out) {
   int n = in.nextInt(), m = in.nextInt();
   boolean[] tab = new boolean[255 * 255 * 2];
   for (int p = 0; p <= m; ++p) {
     for (int q = 0; q <= p; ++q) {
       tab[p * p + q * q] = true;
     }
   }
   List<Integer> lst = new ArrayList<Integer>();
   for (int i = 0; i < tab.length; ++i) {
     if (tab[i]) {
       lst.add(i);
     }
   }
   boolean found = false;
   int b = 1;
   while (true) {
     if ((n - 1) * b > m * m * 2) {
       break;
     }
     for (int idx = 0; idx + n <= lst.size(); ++idx) {
       int a = lst.get(idx);
       if (a + (n - 1) * b > m * m * 2) {
         break;
       }
       // out.println(a, b);
       boolean flag = true;
       for (int i = n - 1; i >= 0; --i) {
         int x = a + i * b;
         if (!tab[x]) {
           flag = false;
           break;
         }
       }
       if (flag) {
         out.println(a, b);
         found = true;
       }
     }
     b++;
   }
   if (!found) {
     out.println("NONE");
   }
 }
    void solve() {
      n = in.nextInt();
      m = in.nextInt();
      days = new int[n + 1];
      req = new int[m + 1];

      for (int i = 1; i <= n; i++) days[i] = in.nextInt();

      for (int i = 1; i <= m; i++) req[i] = in.nextInt();

      if (!isPossible(n)) {
        out.println(-1);

        return;
      }

      int low, high, mid;

      low = 1;
      high = n;
      mid = 1;

      while (low <= high) {
        mid = low + high >> 1;

        boolean poss = isPossible(mid);

        if (poss) {
          boolean prev = isPossible(mid - 1);

          if (prev) high = mid - 1;
          else break;
        } else low = mid + 1;
      }

      out.println(mid);
    }
    void solve(int testNumber) {
      n = in.nextInt();
      q = in.nextInt();

      tree = new Node[4 * n];

      buildTree(1, 0, n - 1);

      for (int i = 0; i < q; i++) {
        int type, from, to;

        type = in.nextInt();
        from = in.nextInt();
        to = in.nextInt();

        if (type == 0) update(1, 0, n - 1, from, to);
        else out.println(query(1, 0, n - 1, from, to));
      }
    }
    void solve() {
      n = in.nextInt();
      k = in.nextInt();
      bit = new int[n + 1];

      for (int i = 0; i < k; i++) {
        int a, b;

        a = in.nextInt();
        b = in.nextInt();
        add(a, 1);
        add(b + 1, -1);
      }

      int[] ans = new int[n];

      for (int i = 1; i <= n; i++) ans[i - 1] = query(i);

      Arrays.sort(ans);
      out.println(ans[n / 2]);
    }
    void solve(int testNumber) {
      n = in.nextInt();
      m = in.nextInt();
      c = in.nextInt();
      bit = new long[n + 1];

      for (int i = 0; i < m; i++) {
        char[] chars = in.next().toCharArray();

        if (chars[0] == 'S') {
          int left, right, k;

          left = in.nextInt();
          right = in.nextInt();
          k = in.nextInt();

          add(left, k);
          add(right + 1, -k);
        } else out.println(c + query(in.nextInt()));
      }
    }
    void solve(int testNumber) {
      t = in.nextInt();

      while (t-- > 0) {
        n = in.nextInt();
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < n; i++) stack.add(in.nextInt());

        stack.sort(
            new Comparator<Integer>() {
              @Override
              public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
              }
            });

        int total = 0;

        while (true) {
          int size = stack.size();

          if (size == 0) break;
          else if (size == 1) total += stack.pop();
          else if (size == 2) total += stack.pop() + stack.pop();
          else if (size == 3) total += stack.pop() + stack.pop() + stack.pop() * 0;
          else {
            total += stack.pop() + stack.pop();
            stack.pop();
            stack.pop();
          }
        }

        out.println(total);
      }
    }
    void solve(int testNumber) {
      n = in.nextInt();
      gain = new int[n];
      lose = new int[n];

      for (int i = 0; i < n; i++) gain[i] = in.nextInt();

      for (int i = 0; i < n; i++) lose[i] = in.nextInt();

      int max = 0;
      int maxStartingPos = -1;
      boolean isInfinity = false;
      int infinityFrom = -1;
      int crossedN = 0;

      outer:
      for (int start = 0; start < n; ) {
        int curr = gain[start];
        int startingPoint = start;
        int currLen = 1;
        System.out.println(
            "start : "
                + start
                + ", currLen : "
                + currLen
                + ", currMaxLen : "
                + max
                + ", sP : "
                + startingPoint);

        while (start < n && curr >= lose[start]) {
          curr -= lose[start];
          start++;
          currLen++;

          if (start == startingPoint) {
            isInfinity = true;
            infinityFrom = start;

            break outer;
          }

          if (start == n) {
            start = 0;
            crossedN++;
          }

          curr += gain[start];
        }

        if (currLen > max) {
          max = currLen;
          maxStartingPos = startingPoint;
        }

        start++;

        if (start >= n) {
          crossedN++;

          if (crossedN >= 2) break;
        }
      }

      if (isInfinity) out.println("Infinity, from position " + infinityFrom);
      else out.println("Max dist from position " + maxStartingPos);
    }
  static void solve() {
    n = in.nextInt();

    arr = new int[1001][1001];

    for (int i = 0; i < n; i++) {
      int x, y;

      x = in.nextInt() - 1;
      y = in.nextInt() - 1;

      arr[x][y]++;
    }

    int i, j;
    int start = 0;
    long left, right, answer;

    left = right = answer = 0;

    while (start < 1000) {
      i = 0;
      j = start;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 1;

    while (start < 1000) {
      i = start;
      j = 0;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 999;

    while (start >= 0) {
      i = start;
      j = 0;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start--;
    }

    start = 1;

    while (start < 1000) {
      i = 999;
      j = start;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start++;
    }

    out.println(answer);
  }