Hello! I’m pretty new to USACO and algorithmic programming in general, and I was working on “The Lost Cow”, a 2017 Bronze problem. I’m working in Java, and from what I can tell my code is basically the same as the solution, the only difference is that I’m using BufferedReader/PrintWriter instead of Kattio. For some reason the time limit keeps getting exceeded - is it an issue on my end or is there something in my code that’s holding up the program? Thanks!
import java.io.*;
import java.util.StringTokenizer;
public class LostCow{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("lostcow.in"));
PrintWriter pw = new PrintWriter("lostcow.out");
StringTokenizer st = new StringTokenizer(r.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int add = 1;
int direction = 1;
int distance = 0;
int directionDistance = 1;
while (true){
if((direction == 1 && x <= y && y <= (x + directionDistance)) || (direction == -1 && x >= y && y >= x - directionDistance)){
distance += Math.abs(y - x);
pw.println(distance);
break;
} else{
distance += (directionDistance * 2);
direction *= 2;
direction *= -1;
}
}
pw.close();
}
}