原題下載
答案
import java.io.*;
import java.util.*;
public class crossroad {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("crossroad.in"));
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) {
crossings++;
}
// update the last side we have seen the given cow on
lastSeen[index] = current;
}
// print the answer
pw.println(crossings);
pw.close();
}
}
以上就是關(guān)于【USACO 2017 February Contest, Bronze Problem 1. Why Did the Cow Cross the Road】的解答,如需了解學(xué)校/賽事/課程動(dòng)態(tài),可至翰林教育官網(wǎng)獲取更多信息。
往期文章閱讀推薦:
2027 USAAIO人工智能奧賽報(bào)名進(jìn)行中!Round 1新規(guī)出爐,2月開(kāi)賽如何備考?
外灘大會(huì)見(jiàn)證AI爆發(fā)!緊跟智能時(shí)代,從USACO計(jì)算機(jī)奧賽起步!

? 2026. All Rights Reserved. 滬ICP備2023009024號(hào)-1