Skip to content

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

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Here we only have to sort the cows and simulate the process.

Source codes#

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

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

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

    int n;
    cin >> n;

    vector<pair<int, int> > vp(n);
    for(int i = 0; i < n; i++)
        cin >> vp[i].first >> vp[i].second;

    sort(vp.begin(), vp.begin() + n);
    int timemax = 0;
    for(int i = 0; i < n; i++)
    {
        timemax = max(timemax, vp[i].first);
        timemax += vp[i].second;
    }

    cout << timemax;
    return 0;
}
with open("cowqueue.in", "r") as fin:
    n = int(fin.readline().strip())
    vp = []
    for _ in range(n):
        a, d = map(int, fin.readline().split())
        vp.append((a, d))
vp.sort(key=lambda x: x[0])
timemax = 0
for a, d in vp:
    timemax = max(timemax, a)
    timemax += d
with open("cowqueue.out", "w") as fout:
    fout.write(str(timemax))