Category Archives: Competitive Programming 3

UVA 12372 – Packing for Holiday

I tried to use three sc.nextInt()‘s inside the if statement. That failed terribly :(

Super easy problem otherwise.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int T = sc.nextInt();
    for (int i = 1; i <= T; i++) {
      out.print("Case " + i + ": ");
      int l = sc.nextInt();
      int w = sc.nextInt();
      int h = sc.nextInt();
      if (l <= 20 && w <= 20 && h <= 20) {
        out.println("good");
      } else {
        out.println("bad");
      }
    }

    out.close();
    sc.close();
  }

}

UVA 12289 – One-Two-Three

Super easy problems indeed. Simple if statements required.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int N = sc.nextInt();
    while (N > 0) {
      String input = sc.next();
      if (input.length() == 5) {
        out.println("3");
      } else {
        if ((input.charAt(0) == 'o' && input.charAt(1) == 'n') || (input.charAt(0) == 'o' && input.charAt(2) == 'e') || (input.charAt(1) == 'n' && input.charAt(2) == 'e')) {
          out.println("1");
        } else {
          out.println("2");
        }
      }
      N--;
    }
    out.close();
    sc.close();
  }

}

UVA 12279 – Emoogle Balance

Super simple problem. Positive Numbers – Negative Numbers.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int count = 1;
    int X = 0;
    while ((X = sc.nextInt()) != 0) {
      int ans = 0;
      for (int i = 0; i < X; i++) {
        if (sc.nextInt() > 0)
          ans++;
        else
          ans--;
      }
      out.println("Case " + count + ": " + ans);
      count++;
    }

    out.close();
    sc.close();
  }

}

UVA 12250 – Language Detection

I missed the switch statements in Java 7 that can take in Strings as cases for this question. Pretty easy.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int i = 1;
    String line;
    while ((line = sc.nextLine()) != null) {
      line = line.trim();
      if (line.equals("#"))
        break;
      out.print("Case " + i + ": ");
      if (line.equals("HELLO")) {
        out.println("ENGLISH");
      } else if (line.equals("HOLA")) {
        out.println("SPANISH");
      } else if (line.equals("HALLO")) {
        out.println("GERMAN");
      } else if (line.equals("BONJOUR")) {
        out.println("FRENCH");
      } else if (line.equals("CIAO")) {
        out.println("ITALIAN");
      } else if (line.equals("ZDRAVSTVUJTE")) {
        out.println("RUSSIAN");
      } else {
        out.println("UNKNOWN");
      }
      i++;
    }
    out.close();
    sc.close();
  }
}

UVA 11727 – Cost Cutting

Easiest way is to sort and print the middle number.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    for (int i = 1; i <= T; i++) {
      int[] list = { sc.nextInt(), sc.nextInt(), sc.nextInt() };
      Arrays.sort(list);
      out.println("Case " + i + ": " + list[1]);
    }
    out.close();
    sc.close();
  }

}

UVA 11547 – Automatic Answer

I just used a double to avoid rounding errors. Then rounded at the end. Pretty easy.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    while (T > 0) {
      double N = sc.nextDouble();
      double ans = (((N * 567.) / 9. + 7492) * 235. / 47.) - 498;
      ans /= 10;
      ans = Math.abs((long) ans);
      out.println((long) ans % 10);
      T--;
    }
    out.close();
    sc.close();
  }
}

UVA 11498 – Division of Nlogonia

I thought this question was going to be a lot more complicated than it turned out.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int K = 0;
    while ((K = sc.nextInt()) != 0) {
      int N = sc.nextInt();
      int M = sc.nextInt();
      while (K > 0) {
        int X = sc.nextInt();
        int Y = sc.nextInt();
        if (X == N || Y == M) {
          out.println("divisa");
        } else if (X > N) {
          if (Y < M) {
            out.println("SE");
          } else {
            out.println("NE");
          }
        } else {
          if (Y < M) {
            out.println("SO");
          } else {
            out.println("NO");
          }
        }
        K--;
      }
    }
    out.close();
    sc.close();
  }

}

UVA 11364 – Parking

This one involves a bit of math. You will notice that it doesn’t matter where you park unless you park away from all of the shops.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    while (T > 0) {
      int N = sc.nextInt();
      int min = Integer.MAX_VALUE;
      int max = Integer.MIN_VALUE;
      for (int i = 0; i < N; i++) {
        int next = sc.nextInt();
        min = Math.min(min, next);
        max = Math.max(max, next);
      }
      out.println(2 * (max - min));
      T--;
    }
    out.close();
    sc.close();
  }

}

UVA 11172 – Relational Operator

Really simple. Just a simple if-else.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int N = Integer.parseInt(sc.nextLine());
    while (N > 0) {
      int X = sc.nextInt();
      int Y = sc.nextInt();
      if (X < Y) {
        out.println("<");
      } else if (X > Y) {
        out.println(">");
      } else {
        out.println("=");
      }
      N--;
    }
    out.close();
    sc.close();
  }

}

UVA 11044 – Searching for Nessy

I failed by misreading the problem on this one. I thought the sonar beam grid was of size 4 instead of size 3. It cost me two WA’s :(

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int N = Integer.parseInt(sc.nextLine());
    while (N &gt; 0) {
      int X = sc.nextInt();
      int Y = sc.nextInt();
      out.println((long) (Math.ceil((X - 2) * 1.0 / 3) * Math.ceil((Y - 2) * 1.0 / 3)));
      N--;
    }
    out.close();
    sc.close();
  }

}