Skip to content

USACO 2017 February Contest Bronze Division - Why Did the Cow Cross the Road#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Process crossings left to right and see what changes occured.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream cin("crossroad.in");
    ofstream cout("crossroad.out");

    int n, cnt = 0;
    cin >> n;

    vector<int> signs(11, -1);

    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        if(signs[a] != b && signs[a] != -1)
            cnt++;
        signs[a] = b;
    }

    cout << cnt;
    return 0;
}
with open("crossroad.in", "r") as fin:
    n = int(fin.readline().strip())
    cnt = 0
    signs = [-1] * 11
    for _ in range(n):
        a, b = map(int, fin.readline().split())
        if signs[a] != b and signs[a] != -1:
            cnt += 1
        signs[a] = b

with open("crossroad.out", "w") as fout:
    fout.write(str(cnt))