USACO 2017 February Contest, Bronze Problem 1. Why Did the Cow Cross the Road




import java.util.*;
public class crossroad {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(""));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("crossroad.out")));

// read in the number of observations
int n = Integer.parseInt(br.readLine());

// create an array to track the last known appearance of each cow
// lastSeen[cow] == 0 means we haven't seen the cow yet
// lastSeen[cow] == 1 means we have last seen the cow on the left side of the road
// lastSeen[cow] == 2 means we have last seen the cow on the right side of the road
int[] lastSeen = new int[11];

// track the number of crossings that are confirmed
int crossings = 0;
for(int i = 0; i < n; i++) {
// read in the data for one observation
StringTokenizer st = new StringTokenizer(br.readLine());
int index = Integer.parseInt(st.nextToken());
int current = Integer.parseInt(st.nextToken())+1;

// if we have seen a cow already and the last side we saw it on is different
// from the current side, then it is a confirmed crossing
if(lastSeen[index] > 0 && lastSeen[index] != current) {

// update the last side we have seen the given cow on
lastSeen[index] = current;

// print the answer