USACO Section 1.3 Barn Repair

来源:互联网 发布:淘宝店铺如何关闭 编辑:程序博客网 时间:2024/06/11 09:59

本题又是一道需要用到贪心算法求解的题目,题目的原文如下。

原文

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 1834681415161721252627303140414243

OUTPUT FORMAT

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

SAMPLE OUTPUT (file barn1.out)

25

分析


这道题可以用上面这张图来描述,有N个牛棚,有些牛棚里有牛,有些没有,要求在暴风雨夜把所有有牛的牛棚都用木板(长度任意)遮起来,并且要求木板的长度最短,并且木板的数量不能超过M
基本的思路如图中描述,可以先用一块完整长度模板遮盖所有的牛棚,然后依次取出最长的连续的空牛棚,把木板锯断,知道木板的数量达到最大限制M。在实际编码中,由于输入的奶牛位置不一定是按顺序来的,所以要先对奶牛的位置排序,然后求出并保存各个空档的大小。

提交代码

/*ID:PROG: barn1LANG: C++*/#include <fstream>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <map>#include <iostream>using namespace std;int main(){ifstream fin("barn1.in");int M,S,C;fin >> M>> S >> C;vector<int> ocstall(C);for (int i=0;i!=C;i++){fin >> ocstall[i];}sort(ocstall.begin(),ocstall.end());int lenghth = ocstall[ocstall.size()-1] - ocstall[0]+1;vector<int> internal(C-1);for (int i=0;i!=C-1;i++){internal[i] = ocstall[i+1] - ocstall[i] - 1;}sort(internal.begin(),internal.end());for (int i=0;i<M-1;i++){if(internal.size()-1<i)break;lenghth -= internal[internal.size()-1-i];}ofstream fout("barn1.out");fout << lenghth <<endl;return 0;}


提交结果

TASK: barn1LANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.000 secs, 3496 KB]   Test 2: TEST OK [0.000 secs, 3496 KB]   Test 3: TEST OK [0.000 secs, 3496 KB]   Test 4: TEST OK [0.000 secs, 3496 KB]   Test 5: TEST OK [0.005 secs, 3496 KB]   Test 6: TEST OK [0.005 secs, 3496 KB]   Test 7: TEST OK [0.011 secs, 3496 KB]   Test 8: TEST OK [0.005 secs, 3496 KB]   Test 9: TEST OK [0.008 secs, 3496 KB]   Test 10: TEST OK [0.005 secs, 3496 KB]All tests OK.


官方给出的参考答案

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define MAXSTALL 200int hascow[MAXSTALL];intintcmp(const void *va, const void *vb){return *(int*)vb - *(int*)va;}voidmain(void){    FILE *fin, *fout;    int n, m, nstall, ncow, i, j, c, lo, hi, nrun;    int run[MAXSTALL];    fin = fopen("barn1.in", "r");    fout = fopen("barn1.out", "w");        assert(fin != NULL && fout != NULL);    fscanf(fin, "%d %d %d", &m, &nstall, &ncow);    for(i=0; i<ncow; i++) {fscanf(fin, "%d", &c);hascow[c-1] = 1;    }    n = 0;/* answer: no. of uncovered stalls */    /* count empty stalls on left */    for(i=0; i<nstall && !hascow[i]; i++)n++;    lo = i;    /* count empty stalls on right */    for(i=nstall-1; i>=0 && !hascow[i]; i--)n++;    hi = i+1;    /* count runs of empty stalls */    nrun = 0;    i = lo;    while(i < hi) {while(hascow[i] && i<hi)    i++;for(j=i; j<hi && !hascow[j]; j++)    ;run[nrun++] = j-i;i = j;    }    /* sort list of runs */    qsort(run, nrun, sizeof(run[0]), intcmp);    /* uncover best m-1 runs */    for(i=0; i<nrun && i<m-1; i++)n += run[i];    fprintf(fout, "%d\n", nstall-n);    exit(0);}


THE END




0 0
原创粉丝点击