Barn+Repair

USACO 1.3 - 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.

PROGRAM NAME: barn1

INPUT FORMAT

Line 1: M, S, and C (space separated)

Lines 2-C+1: Each line contains one integer, the number of an occupied stall.

SAMPLE INPUT (file barn1.in)

4 50 18

3

4

6

8

14

15

16

17

21

25

26

27

30

31

40

41

42

43

OUTPUT FORMAT

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

SAMPLE OUTPUT (file barn1.out)

25

[One minimum arrangement is one board covering stalls 3-8, one covering 14-21, one covering 25-31, and one covering 40-43.]

Source Code

code format="java" /* ID: wjh3331 LANG: JAVA TASK: barn1

import java.io.*; import java.util.*;

public class barn1 { // 헛간 사이의 공간을 나타내는 클래스 public static class Space implements Comparable { private int index; // 몇번째 공간인지 private int gap; // 공간의 크기

public Space(int index, int gap) { this.index = index; this.gap = gap; }

public int getIndex { return index; }

public int getGap { return gap; }

public int compareTo(Space o) { return o.gap - this.gap; }

@Override public String toString { return Integer.toString(gap); } }

public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("barn1.in")); PrintWriter out = new PrintWriter(new FileWriter("barn1.out"));

StringTokenizer st = new StringTokenizer(in.readLine);

int m = Integer.parseInt(st.nextToken); // 판대기의 개수 int s = Integer.parseInt(st.nextToken); // 헛간의 총 개수 int c = Integer.parseInt(st.nextToken); // 소가 있는 헛간의 개수

// 판대기가 소의 수보다 많으면 소의 수만큼의 길이 필요 if(m >= c) { out.println(c);

out.close; System.exit(0); }

// 소가 있는 헛간들 초기화 및 정렬 int[] occupiedStalls = new int[c]; for(int i = 0; i < c; i++) { occupiedStalls[i] = Integer.parseInt(in.readLine); }   Arrays.sort(occupiedStalls);

// 공간들을 초기화 및 정렬 Queue spaces = new PriorityQueue(c); for(int i = 0; i < c - 1; i++) { spaces.offer(new Space(i, occupiedStalls[i + 1] - occupiedStalls[i])); }

// 그중에 큰 것들만 골라 초기화 및 정렬 Queue bigSpaces = new PriorityQueue; for(int i = 0; i < m - 1; i++) { bigSpaces.offer(spaces.poll.getIndex); }

// 판대기 설치하고 길이를 다 더함 int sum = 0; int start = 0; int size = bigSpaces.size; for(int i = 0; i < size; i++) { int j = bigSpaces.poll; sum += occupiedStalls[j] - occupiedStalls[start] + 1; start = j + 1; }   sum += occupiedStalls[c - 1] - occupiedStalls[start] + 1;

// 결과 출력 out.println(sum);

out.close; System.exit(0); } } code

Comment 문제의 번호가 더해질수록... 문제조차 이해하기 힘들어지는 이 현실... 우선순위 큐를 이용해봤습니다. 그래도 허접하게 짜서 코드가 깔끔해졌다는 생각이 안드네요;; 방금 프리토킹을 다시 읽어봤는데... 한지연님께서 말씀하신대로 수행시간도 같이 올리겠습니다.(이전것들도...) 거부기 속도와 물량 메모리에 쪽팔리긴 하지만 정확한 측정을 위해선 지연님 말씀이 맞는것 같네요...