USACO Bronze 2016 January - Angry Cows
Author: Qi Wang
Video Solution
By Qi Wang
Explanation
Since there's at most 100 haybales, we can try launching a cow at every single one and simulate the aftermath.
Implementing the explosions can get a little nasty, though, so to make things easier we can notice that the explosions on the left and right side are completely independent from each other.
Consider a situation where the haybales are . We'll refer to them by their positions for convenience.
If we launch a cow at the haybale , although is able to blow up with an explosion of radius , was already going to explode anyway from 's explosion of radius .
This observation allows us to check the farthest the explosion can reach on both sides and then add up the results for our final answer.
Warning!
The solution from the official analysis is incorrect on the following input:
3 3 5 6
It outputs 2, but the correct answer is 3.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int N;vector<int> bales;int exploded_num(int start, int direction) {int radius = 1;int prev = start;while (true) {
Java
import java.io.*;import java.util.*;public class Angry {private static int N;private static int[] bales;public static void main(String[] args) throws IOException {Kattio io = new Kattio("angry");
Python
with open("angry.in") as read:bales = sorted([int(read.readline()) for _ in range(int(read.readline()))])def exploded_num(start: int, direction: int) -> int:radius = 1prev = startwhile True:next_ = prev# Get the furthest explosion index without exceeding the current radius
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!