Problem Info
USACO 2015 December Contest, Bronze Problem 2 Speeding Ticket
USACO
http://www.usaco.org/index.php?page=viewproblem2&cpid=568
My Work
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Speeding {
static int getMax(int[] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= max) {
max = arr[i];
}
}
return max;
}
static int getSpeed(int mi, ArrayList<Integer> marks, ArrayList<Integer> speeds) {
// getting speed with the mile and the corresponding speed
int ind = -1;
int curr = 0;
for (int i = 0; i < marks.size(); i++) {
ind++;
curr += marks.get(i);
if (marks.get(i) >= mi) {
break;
}
}
return speeds.get(ind);
}
static int getDiff(int mi, ArrayList<Integer> limits, ArrayList<Integer> limitmarks, ArrayList<Integer> speeds, ArrayList<Integer> speedmarks) {
return getSpeed(mi, speedmarks, speeds) - getSpeed(mi, limitmarks, limits);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("speeding.in"));
PrintWriter pw = new PrintWriter("speeding.out");
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> limits = new ArrayList<Integer>();
ArrayList<Integer> limitmarks = new ArrayList<Integer>();
ArrayList<Integer> speeds = new ArrayList<Integer>();
ArrayList<Integer> speedmarks = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
limitmarks.add(x);
limits.add(y);
}
for (int i = 0; i < m; i++) {
int z = sc.nextInt();
int q = sc.nextInt();
speedmarks.add(z);
speeds.add(q);
}
sc.close();
int[] maxs = new int[100];
for (int i = 1; i <= 100; i++) {
maxs[i-1] = getDiff(i, limits, limitmarks, speeds, speedmarks);
}
int ans = getMax(maxs);
if (ans < 0) {
ans = 0;
}
pw.println(ans);
pw.close();
}
}
I basically look at every individual mile and get the speed difference
.
What I’ve Tried
I submit the code but failed 3 test cases. I’ve tried testing out random values but I have gotten the correct answer.
Question
I don’t know why the code works on 7 test cases but fails on 3 other.