Just do it in two cases. Where a < b and where b < a. [java] import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P12468 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNextInt()) { int a = sc.nextInt(); int b = sc.nextInt(); if (a == -1) break; if (b < a) { out.println(Math.min(a - b, 100 - a + b)); } else { out.println(Math.min(b - a, 100 - b + a)); } out.flush(); } out.close(); sc.close(); } } [/java]
Tag Archives: java
UVA 12157 – Tariff Plan
I had initially accidentally written “Case #1” instead of “Case 1” :[
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P12157 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for (int zz = 1; zz <= T; zz++) { int N = sc.nextInt(); int mileCost = 0; int juiceCost = 0; for (int i = 0; i < N; i++) { int talkTime = sc.nextInt(); mileCost += (int) Math.ceil(talkTime / 30.0) * 10; if (talkTime % 30 == 0) mileCost += 10; juiceCost += (int) Math.ceil(talkTime / 60.0) * 15; if (talkTime % 60 == 0) juiceCost += 15; } out.print("Case " + zz + ": "); if (mileCost < juiceCost) { out.println("Mile " + mileCost); } else if (juiceCost < mileCost) { out.println("Juice " + juiceCost); } else { out.println("Mile Juice " + mileCost); } } out.close(); sc.close(); } }
UVA 12015 – Google is Feeling Lucky
Simple!
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P12015 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); ArrayList<String> ans; int N = sc.nextInt(); for (int i = 1; i <= N; i++) { int max = -1; ans = new ArrayList<String>(); for (int j = 0; j < 10; j++) { String tmp = sc.next(); int val = sc.nextInt(); if (val > max) { ans.clear(); ans.add(tmp); max = val; } else if (val == max) { ans.add(tmp); } } out.println("Case #" + i + ":"); for (String t : ans) { out.println(t); } } out.close(); sc.close(); } }
UVA 11942 – Lumberjack Sequencing
Two linear scans to figure out if its ascending or descending. If one of them works then Ordered else Unordered.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(); out.println("Lumberjacks:"); for (int i = 0; i < N; i++) { int[] arr = new int[10]; for (int j = 0; j < 10; j++) arr[j] = sc.nextInt(); int bad = 0; for (int j = 1; j < 10; j++) { if (arr[j - 1] > arr[j]) { bad++; break; } } for (int j = 1; j < 10; j++) { if (arr[j - 1] < arr[j]) { bad++; break; } } if (bad == 1) { out.println("Ordered"); } else { out.println("Unordered"); } } out.close(); sc.close(); } }
UVA 11799 – Horror Dash
Clowns must run at (Math.max)imum speed! :D
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11799 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int N = Integer.parseInt(br.readLine()); for (int i = 1; i <= N; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int ans = 0; while (st.hasMoreTokens()) { ans = Math.max(ans, Integer.parseInt(st.nextToken())); } out.println("Case " + i + ": " + ans); } out.close(); br.close(); } }
UVA 11764 – Jumping Mario
Super simple linear scan.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11764 { 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 N = sc.nextInt(); int high = 0; int low = 0; int last = sc.nextInt(); for (int j = 1; j < N; j++) { int t = sc.nextInt(); if (t < last) { low++; } else if (t > last) { high++; } last = t; } out.println("Case " + i + ": " + high + " " + low); } out.close(); sc.close(); } }
UVA 11679 – Sub-prime
I was doing this wayy too complicatedly before using an ArrayList and a counter to see if I could greedily find the appropriate match or not. The easiest way I realized mid coding process was to just let the transactions go through and then check at the end if everyone ends up with a positive or 0 balance.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11679 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNextInt()) { int B = sc.nextInt(); int N = sc.nextInt(); if (B == N && B == 0) break; int[] reserves = new int[B]; for (int i = 0; i < reserves.length; i++) reserves[i] = sc.nextInt(); for (int i = 0; i < N; i++) { int from = sc.nextInt() - 1; int to = sc.nextInt() - 1; int amt = sc.nextInt(); reserves[from] -= amt; reserves[to] += amt; } if (isCool(reserves)) { out.println("S"); } else { out.println("N"); } } out.close(); sc.close(); } private static boolean isCool(int[] reserves) { for (int i = 0; i < reserves.length; i++) { if (reserves[i] < 0) return false; } return true; } }
UVA 11559 – Event Planning
This was fun. First program so far for which I used an extra class. Easy problem. 8)
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 P11559 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNextInt()) { int N = sc.nextInt(); int B = sc.nextInt(); int H = sc.nextInt(); int W = sc.nextInt(); Hotel[] hotels = new Hotel[H]; for (int i = 0; i < H; i++) { hotels[i] = new Hotel(); hotels[i].price = sc.nextInt(); for (int j = 0; j < W; j++) { hotels[i].maxBeds = Math.max(hotels[i].maxBeds, sc.nextInt()); } } Arrays.sort(hotels); boolean found = false; for (int i = 0; i < H; i++) { if (hotels[i].price * N <= B && hotels[i].maxBeds >= N) { out.println(hotels[i].price * N); found = true; break; } } if (!found) out.println("stay home"); } out.close(); sc.close(); } private static class Hotel implements Comparable<Hotel> { int price; int maxBeds; @Override public int compareTo(Hotel o2) { if (this.price == o2.price) { return Integer.compare(o2.maxBeds, this.maxBeds); } else { return Integer.compare(this.price, o2.price); } } } }
UVA 11332 – Summing Digits
Yay recursion!
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11332 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (true) { long x = sc.nextLong(); if (x == 0) break; out.println(solve(x)); } out.close(); sc.close(); } private static long solve(long x) { if (x < 10) return x; long sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return solve(sum); } }
UVA 10963 – The Swallowing Ground
Really really simple problem. Need to remember not to make incorrect assumptions. Problem gets easily solved once you use absolute values and you read the question again which tells you to print a blank line between outputs. :[
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P10963 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int W = sc.nextInt(); for (int i = 0; i < W; i++) { int C = sc.nextInt(); boolean bad = false; int last = Math.abs(sc.nextInt() - sc.nextInt()); for (int j = 1; j < C; j++) { int tmp = Math.abs(sc.nextInt() - sc.nextInt()); if (last != tmp) bad = true; } if (bad) { out.println("no"); } else { out.println("yes"); } if (i < W-1) out.println(); } out.close(); sc.close(); } }