USACO Silver 2018 December - Convention

Authors: Qi Wang, Ryan Chou

Table of Contents

ExplanationImplementation

Official Analysis (C++)

Explanation

Consider a possible maximum waiting time XX, if we can't file all of the cows into the buses with this constraint, we can't process the cows with a smaller XX either. It remains to find how to check if some XX can be processed.

Instead of the problem being:

Is it possible to file the cows in MM buses with a maximum waiting time of XX?

We'll transform the problem into:

What's the minimum number of buses needed to transport the cows with a maximum waiting time of XX?

This simplification allows us to break processing a cow into three cases:

  1. Adding this cow will cause the first cow to exceed the maximum waiting time.
  2. Adding this cow will overflow the bus capacity.
  3. Adding this cow will satisfy all of the constraints.

It takes O(logN)\mathcal{O}(\log N) time to binary search on XX and O(N)\mathcal{O}(N) time to validate a possible time constraint, so this leaves us with a O(NlogN)\mathcal{O}(N\log N) solution, which fits comfortably under the time limit.

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N\log N)

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;

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!