출처: https://train.usaco.org/usacogate


문제 설명

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.

출력

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

입력 예

4 50 18 
3 
4 
6 
8 
14 
15 
16 
17 
21 
25 
26 
27 
30 
31 
40 
41 
42 
43

출력 예

25


문제 풀이

소들을 순서대로 정렬한다. 제일 처음과 제일 마지막 소를 다 가릴 수 있는 긴 보드에서 시작한다. 각 소들 사이에 간격들을 계산해서 정렬한다. 가장 넓은 간격부터 긴 보드에서 제거해나가서 필요한 보드를 완성한다 

프로그램 내용

더보기
#define MAX_BOARD 50
#define MAX_STALL 200

int main() {
    ifstream fin ("barn1.in");
    ofstream fout ("barn1.out");

    /// Read requirement
    int num_boards = 0, num_stalls =0, num_cows = 0;
    fin >> num_boards >> num_stalls >> num_cows ;

    vector <int> filled_stalls;
    vector <pair <int, int>> gap_table;

    /// reading filled stalls
    for(int idx=0; idx < num_cows; ++idx)
    {
		...
	}

    /// sorting stalls
    sort(filled_stalls.begin(),filled_stalls.end()); 

    for(int idx=1; idx < num_cows; ++idx)
    {
        gap_table.push_back(make_pair(filled_stalls[idx-1], filled_stalls[idx]-filled_stalls[idx-1]-1));
    }

    /// checked end to end
    int base_line = 0;
    base_line = filled_stalls[num_cows-1] - filled_stalls[0] +1;

    /// sort gap from longest
    sort(gap_table.begin(),gap_table.end()); 

    /// remove num_boards - 1 gaps from base_line
    for(int idx=0; idx < num_boards - 1; ++idx)
    {
        if(base_line - gap_table[idx].second > 0)
            base_line -= gap_table[idx].second;
    }

    /// result
    fout << base_line << endl;

 

Chapter 1. Getting started 

 

'USACO Training' 카테고리의 다른 글

Problem 1.4.6 Combination Lock  (0) 2019.09.12
Problem 1.4.5 Prime Cryptarithm  (0) 2019.09.12
Problem 1.4.2 Mixing Milk  (0) 2019.09.12
Problem 1.3.6 Dual Palindromes  (0) 2019.09.08
Problem 1.3.5 Palindromic Squares  (0) 2019.09.08

+ Recent posts