Barn Repair

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.] 

代码:

/*
 ID: jszhais1
 PROG: barn1
 LANG: C++
 */

#include <fstream>
using namespace std;

struct Max_difference
{
    int behind;
    int value;
};

int cmp( const void *a ,const void *B)
{
    return ((Max_difference *)a)->value < ((Max_difference *)B)->value ? 1 : -1;
}

int cmp_stall ( const void *a , const void *b )
{
    return *(int *)a - *(int *)b;
}

int cmp_behind( const void *a ,const void *B)
{
    return ((Max_difference *)a)->behind > ((Max_difference *)B)->behind ? 1 : -1;
}

int main(int argc, const char * argv[])
{
    ofstream fout("barn1.out");
    ifstream fin("barn1.in");
    int M,S,C,stall_number[200],key=0;
    Max_difference dif[200];
    fin>>M>>S>>C;
    for(int i=0;i<C;i++)
    {
        fin>>stall_number[i];
    }
    qsort(stall_number,C,sizeof(stall_number[0]),cmp_stall);
    for(int i=0;i<C-1;i++)
    {
        dif[i].value = stall_number[i+1] - stall_number[i];
        dif[i].behind = i+1;
    }
    qsort(dif,C-1,sizeof(dif[0]),cmp);
    if(M-1<=C-1)
        qsort(dif,M-1,sizeof(dif[0]),cmp_behind);
    else
        qsort(dif,C-1,sizeof(dif[0]),cmp_behind);

    int start=stall_number[0];
    for(int i=0;i<M-1&&i<C-1;i++)
    {
        key += stall_number[dif[i].behind-1] - start+1;
        start = stall_number[dif[i].behind];
    }
    key += stall_number[C-1]-start+1;
    fout<<key<<endl;
    return 0;
}
思路:

此题可用贪心算法,先对输入的数据按从小到大进行排序,将排好序的数做差并将结果存入另一个数组a,将数组a从大到小排序。

此题应注意M大于C的情况(非常易出错)

结果:

USER: jim zhai [jszhais1]
TASK: barn1
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3356 KB]
   Test 2: TEST OK [0.000 secs, 3356 KB]
   Test 3: TEST OK [0.000 secs, 3356 KB]
   Test 4: TEST OK [0.000 secs, 3356 KB]
   Test 5: TEST OK [0.000 secs, 3356 KB]
   Test 6: TEST OK [0.000 secs, 3356 KB]
   Test 7: TEST OK [0.000 secs, 3356 KB]
   Test 8: TEST OK [0.000 secs, 3356 KB]
   Test 9: TEST OK [0.000 secs, 3356 KB]
   Test 10: TEST OK [0.000 secs, 3356 KB]

All tests OK.

Your program ('barn1') produced all correct answers! This is your submission #4 for this problem. Congratulations!


答案:

Barn Repair
Russ Cox

If we can purchase M boards, then we can leave unblocked M-1 runs of stalls without cows in them, in addition to any stalls on the leftmost side that don't have cows and any stalls on the rightmost side that don't have cows.

We input the list of cows in stalls, storing into an array whether or not there is a cow in a particular stall. Then we walk the array counting sizes of runs of cowless stalls. We sort the list of sizes and pick the M-1 largest ones as the stalls that will remain uncovered.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXSTALL 200
int hascow[MAXSTALL];

int
intcmp(const void *va, const void *vb)
{
	return *(int*)vb - *(int*)va;
}

void
main(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);
}
Alexandru Tudorica's solution might be simpler:
var f:text;
    a,b:array[1..1000] of longint;
    i,m,s,c,k:longint;

procedure qsort(l,r:longint);
    var i,j,x,y:longint;
begin
     i:=l; j:=r; x:=a[(l+r) div 2];
     repeat
           while a[i]<x do i:=i+1;
           while x<a[j] do j:=j-1;
           if i<=j then begin
                y:=a[i]; a[i]:=a[j]; a[j]:=y;
                i:=i+1;
                j:=j-1;
           end;
     until i>j;
     if l<j then qsort(l,j);
     if i<r then qsort(i,r);
end;

procedure qsortb(l,r:longint);
    var i,j,x,y:longint;
begin
     i:=l; j:=r; x:=b[(l+r) div 2];
     repeat
           while b[i]<x do i:=i+1;
           while x<b[j] do j:=j-1;
           if i<=j then
           begin
                y:=b[i]; b[i]:=b[j]; b[j]:=y;
                i:=i+1;
                j:=j-1;
           end;
     until i>j;
     if l<j then qsortb(l,j);
     if i<r then qsortb(i,r);
end;


begin
     assign(f,'barn1.in');
     reset(f);
     readln(f,m,k,c);
     for i:=1 to c do readln(f,a[i]);
     qsort(1,c);
     for i:=1 to c-1 do b[i]:=a[i+1]-a[i]-1;
     qsortb(1,c-1);
     for i:=c-1 downto (c-m+1) do s:=s+b[i];
     close(f);
     assign(f,'barn1.out');
     rewrite(f);
     writeln(f,a[c]-a[1]-s+1);
     close(f);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值