USACO 1.3.2 Barn Repair

It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John’s cows. Happily, many of the cows were on vacation, so the barn was not completely full.

The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.

Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.

Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.

Print your answer as the total number of stalls blocked.



Line 1: M, S, and C (space separated)
Lines 2-C+1: Each line contains one integer, the number of an occupied stall.


4 50 18


A single line with one integer that represents the total number of stalls blocked.

SAMPLE OUTPUT (file barn1.out)



This is my algorithm for this problem…

  1. Sort stall numbers
  2. Find gaps between stalls
  3. Sort gaps
  4. Assume that each consecutive stall with cows is covered by a board
  5. Cover the gaps one by one (starting from the smallest) by merging boards
  6. Do this until the number of boards is equal to the maximum number of boards that can be purchased


We first imagine that each consecutive stall with cows is covered by a board.  This means that every stall is covered, and since no stalls without cows are covered, this is the minimum number of covered stalls that must be blocked.  This may be the minimum number but it uses too many boards.  In order to reduce the number of boards used, we merge the boards between the smallest gaps.  If we do it between the smallest gaps, it means that the least number of stalls are covered in merging the boards.  Each time two boards are merged, our number of boards used reduces by one.  We continuously do this until the number of boards we are using is equal to M (the maximum boards we can purchase).

This algorithm is greedy because we are using the minimum number of covered stalls for N boards to find the minimum number of covered stalls for N – 1 boards.

Here is my implementation in Java (I’m sorry it’s not very transferrable into other languages because of the use of ArrayList but it’s all I could come up with)…

ID: frigid.1
TASK: barn1

   import java.util.*;

   class Gap implements Comparable<Gap> {
      int start, end;

      Gap (int s, int e) {
         start = s;
         end = e;

      public int compareTo (Gap o) {
         return getSize() - o.getSize();

      public int getSize () {
         return end - start - 1;

   } //Gap class

   public class barn1 {

      public static void main (String [] args) throws IOException {

         BufferedReader f = new BufferedReader(new FileReader(""));
         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("barn1.out")));

         StringTokenizer st = new StringTokenizer (f.readLine());
         Gap g;

         int m = Integer.parseInt (st.nextToken());
         int s = Integer.parseInt (st.nextToken());
         int c = Integer.parseInt (st.nextToken());
         int boards, stallsCovered = c;

         int [] stalls = new int [c];
         ArrayList<Gap> gaps = new ArrayList<Gap>();

         for (int i = 0; i < c; i++)
            stalls[i] = Integer.parseInt (f.readLine());

			//sort stalls

         Arrays.sort (stalls);

			//determine gaps

         for (int i = 1; i < c; i++) {
            if (stalls[i] - stalls[i - 1] > 1) {
               gaps.add (new Gap (stalls[i - 1], stalls[i]));

			//sort gaps

         Collections.sort (gaps);

			//number of boards needed is # of gaps + 1

         boards = gaps.size() + 1;

         while (boards > m) {
				//remove smallest gap
            g = gaps.remove(0);
				//add its size to stallsCovered
            stallsCovered += g.getSize();
				//remove a board

         out.println (stallsCovered);

      } //main method
   } //barn1 class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: