Category Archives: Competitive Programming 3

UVA 10550 – Combination Lock

A bit of math but an otherwise really simple problem. I would have voted this as harder than the earlier problems as you need to figure out certain numbers based on the direction you are rotating.

import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P10550 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    while (sc.hasNextLine()) {
      int degree = sc.nextInt();
      int first = sc.nextInt();
      int second = sc.nextInt();
      int third = sc.nextInt();
      if (first == second && second == third && third == degree && degree == first) {
        break;
      } else {
        out.println(doSolve(degree, first, second, third));
      }
    }
    out.close();
    sc.close();
  }

  private static long doSolve(int degree, int first, int second, int third) {
    // 40 numbers = 360 degrees. 1 number = 9 degrees.
    long ans = 0;
    // 2 clockwise turns.
    ans += 720;
    // To first number
    ans += 9 * findWay(degree, first, 0);
    // One counterclockwise turn
    ans += 360;
    // To second number
    ans += 9 * findWay(first, second, 1);
    // To third number
    ans += 9 * findWay(second, third, 0);
    return ans;
  }

  private static int findWay(int from, int to, int cw) {
    //Could memo this technically.
    
    int count = 0;
    if (cw == 1) {
      while (from != to) {
        from++;
        if (from == 40)
          from = 0;
        count++;
      }
    } else {
      while (from != to) {
        from--;
        if (from == -1)
          from = 39;
        count++;
      }
    }
    return count;
  }
}

UVA 1124 – Celebrity Jeopardy

Just re-print the input. There has to be a cooler way to do this in Java. I really want to just pipe the inputstream to the outputstream somehow.

import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P1124 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    while (sc.hasNextLine()) {
      out.println(sc.nextLine());
    }
    out.close();
    sc.close();
  }

}

UVA 272 – TEX Quotes

This problem is super easy beacuse we are guaranteed that every quote is there an even number of times. Just replace the odd and even quotes in a different manner and you are set!

import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P272 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    boolean even = true;
    while (sc.hasNextLine()) {
      char[] line = sc.nextLine().toCharArray();
      for (Character c : line) {
        if (c == '"') {
          if (even) {
            out.print("``");
          } else {
            out.print("''");
          }
          even = !even;
        } else {
          out.print(c);
        }
      }
      out.print(System.lineSeparator());
    }
    out.close();
    sc.close();
  }

}

Competitive Programming 3

So I finally shelled out $40 and bought this book, I’ve been wanting to buy it for a while so I went all out and bought the hardcover. Books pretty well written and a definite improvement over the first edition. The first edition is free now by the way; you can either donate to lulu and get it or you can just download it for free. You’ll still learn a lot from the first edition so I recommend that people read up on that if they can’t afford to buy this most recent edition.

Competitive Programming 3
Competitive Programming 3

The questions mentioned in the book are available on the UVA Online Judge so if you just want to practice they are all there. However, you will seriously miss out on all the insights and tips that the authors share. I highly recommend that people buy the book if they want to get better like I do. I’m giong to be going through every single question in this book and see how it effects my ranking on Codeforces and Topcoder. I’ll be posting solutions to all the UVA Questions mentioned in the book in case anyone is interested. The link for the free first edition can be found on the authors booksite :)

My solutions to the questions in the book are listed in the “Competitive Programming 3” page over on the right side of this page.