USACO 2017 January Contest Bronze Division - Don't Be Last!#
Problem link: here
Solution Author: Stefan Dascalescu
Problem Solution#
Simple brute force problem, one just needs to process the data and be careful with finding the second lowest distinct number.
Source codes#
The source codes in C++ and Python can be seen below.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream cin("notlast.in");
ofstream cout("notlast.out");
vector<string> cows = {"Bessie", "Elsie", "Daisy", "Gertie", "Annabelle", "Maggie", "Henrietta"};
vector<int> cnt(7);
int n;
cin >> n;
for(; n; n--)
{
string a;
int b;
cin >> a >> b;
for(int i = 0; i < 7; i++)
if(a == cows[i])
cnt[i] += b;
}
vector<int> cnt2 = cnt;
sort(cnt2.begin(), cnt2.end());
int quantity = -1;
for(int i = 1; i < 7; i++)
if(cnt2[i] > cnt2[i-1])
{
quantity = cnt2[i];
break;
}
vector<string> ans;
for(int i = 0; i < 7; i++)
if(cnt[i] == quantity)
ans.push_back(cows[i]);
if(ans.size() == 1)
cout << ans[0];
else
cout << "Tie";
return 0;
}
with open("notlast.in", "r") as fin:
lines = fin.read().splitlines()
n = int(lines[0])
cows = ["Bessie", "Elsie", "Daisy", "Gertie", "Annabelle", "Maggie", "Henrietta"]
cnt = [0] * 7
for i in range(1, n + 1):
parts = lines[i].split()
cow, points = parts[0], int(parts[1])
for j in range(7):
if cow == cows[j]:
cnt[j] += points
break
cnt2 = cnt.copy()
cnt2.sort()
quantity = -1
for i in range(1, 7):
if cnt2[i] > cnt2[i - 1]:
quantity = cnt2[i]
break
ans = []
for i in range(7):
if cnt[i] == quantity:
ans.append(cows[i])
with open("notlast.out", "w") as fout:
if len(ans) == 1:
fout.write(ans[0])
else:
fout.write("Tie")