USACO Silver 2018 December - Convention
Authors: Qi Wang, Ryan Chou
Explanation
Consider a possible maximum waiting time , if we can't file all of the cows into the buses with this constraint, we can't process the cows with a smaller either. It remains to find how to check if some can be processed.
Instead of the problem being:
Is it possible to file the cows in buses with a maximum waiting time of ?
We'll transform the problem into:
What's the minimum number of buses needed to transport the cows with a maximum waiting time of ?
This simplification allows us to break processing a cow into three cases:
- Adding this cow will cause the first cow to exceed the maximum waiting time.
- Adding this cow will overflow the bus capacity.
- Adding this cow will satisfy all of the constraints.
It takes time to binary search on and time to validate a possible time constraint, so this leaves us with a solution, which fits comfortably under the time limit.
Implementation
Time Complexity:
Java
import java.io.*;import java.util.*;public class convention {static int N;static int numBus;static int cap;static int[] cow;public static void main(String[] args) throws IOException {InputReader in = new InputReader("convention.in");
C++
#include <algorithm>#include <cstdio>#include <iostream>#include <vector>using std::endl;using std::vector;int n, m, c;vector<int> arrivals;
Python
def validate(k: int) -> bool:bus = 0 # number of buses neededcow = 0 # current cowlcow = 0 # earliest cow on this buswhile cow < n:if cow == lcow:bus += 1# can't satisfy time constraint by adding this cow
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!