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

Barn Repair 修理牛棚(翻译摘自http://www.cnblogs.com/AbandonZHANG/archive/2012/05/25/2598283.html)

在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚没有住满。 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。 自门遗失以后,farmer John必须尽快在牛棚之前竖立起新的木板。 他的新木材供应商将会供应他任何他想要的长度,但是吝啬的供应商只能提供有限数目的木板。 farmer John想将他购买的木板总长度减到最少。描述

给出:可能买到的木板最大的数目M(1<= M<=50);牛棚的总数S(1<= S<=200); 牛棚里牛的总数C(1 <= C <=S);和牛所在的牛棚的编号stall_number(1 <= stall_number <= S),计算拦住所有有牛的牛棚所需木板的最小总长度。 输出所需木板的最小总长度作为答案。

格式

PROGRAM NAME: barn1

INPUT FORMAT:

(file barn1.in)

1 行: 木板最大的数目M ,牛棚的总数S 和 牛的总数C(用空格分开)  
2 到 C+1行:  每行包含一个整数,表示牛所占的牛棚的编号。

OUTPUT FORMAT:

(file barn1.out)

单独的一行包含一个整数表示所需木板的最小总长度。

SAMPLE INPUT

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


SAMPLE OUTPUT

25

[ 一种最优的安排是用板拦牛棚3-8,14-21,25-31,40-43.]

根据可购木板数量M,在牛栏中去除前M-1大的相邻牛栏的间距,则自然将牛栏分为M区,且需木板总长最小。

/*
ID: ******
PROG: barn1
LANG: C++
*/
#include <iostream> 
#include <fstream> 
#include <string>
#include <queue>
#include <cstdio>
using namespace std; 
int st[201];
int comp(const void *a,const void *b)//定义递增排序
{
return *(int *)a-*(int *)b;
}
int main() 
{  
ofstream fout ("barn1.out");
ifstream fin ("barn1.in");
priority_queue<int>q;//大顶堆
int M,S,C,i,temp,count;
fin>>M>>S>>C;
for(i=0;i<C;i++)
fin>>st[i];
qsort(st,C,sizeof(st[0]),comp);//从小到大排序
for(i=0;i<C-1;i++)//计算各个牛栏与下一牛栏的间距
{
temp=st[i+1]-st[i];
q.push(temp);
}
if(C>=2)
{
count=st[C-1]-st[0]+1;
M--;
if(M<C)
{while(M)
{
count-=(q.top()-1);
q.pop();
M--;
}
}
else
count=C;


}
else
count=1;//如果仅有一个牛栏则只需1
fout<<count<<endl;
return 0; 



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值