Barn Repair修理牛棚

来源:互联网 发布:电脑删除数据恢复 编辑:程序博客网 时间:2024/04/28 01:59

 Barn Repair修理牛棚


在一个暴风雨的夜晚,农民约翰的牛棚的屋顶、 门被吹飞了. 好在许多牛正在度假,所以牛棚没有住
满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有牛,有些没有. 所有的牛棚有相
同的宽度. 自门遗失以后,农民约翰很快在牛棚之前竖立起新的木板. 他的新木材供应者将会供应
他任何他想要的长度,但是供应者只能提供有限数目的木板. 农民约翰想将他购买的木板总长度减
到最少. 给出 M(1<= M<=50),可能买到的木板最大的数目;S(1<= S<=200),牛棚的总数;C(1 <= C
<=S) 牛棚里牛的数目,和牛所在的牛棚的编号 stall_number(1 <= stall_number <= S),计算拦住
所有有牛的牛棚所需木板的最小总长度. 输出所需木板的最小总长度作为的答案. 
PROGRAM NAME: barn1 
INPUT FORMAT 
第 1 行: M , S 和 C(用空格分开)  
第 2 到 C+1 行:  每行包含一个整数,表示牛所占的牛棚的编号.  
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 
单独的一行包含一个整数表示所需木板的最小总长度.  

SAMPLE OUTPUT (file barn1.out) 
25    {一种最优的安排是用板拦住牛棚 3-8,14-21,25-31,40-43.}

==================================

注意:

对于一般性的需要利用顺序的题目,一定要在程序中先排序...

不要相信出题人会把顺序给你排好,让你做....这些细节千万不要出错...

=================

{ID:jie19952PROG:barn1LANG:PASCAL}var  m,s,c:longint;  np,np_:array[1..200]of longint;procedure init;begin  assign(input,'barn1.in');  assign(output,'barn1.out');  reset(input); rewrite(output);end;procedure terminate;begin  close(input); close(output);  halt;end;procedure main;var  i,j,n,tem,t:longint;//  n:longint;begin  readln(m,s,c);  for i:=1 to c do    readln(np[i]);  for i:=1 to c-1 do    for j:=i+1 to c do      if np[i]>np[j] then        begin          tem:=np[i];          np[i]:=np[j];          np[j]:=tem;        end;  n:=1; //所用木块个数...  s:=np[c]-np[1]+1; //所用木块总长度...  for i:=2 to c do    begin      np_[i]:=np[i]-np[i-1]-1; //i与i+1的间隔长度...      //if np_[i]=0 then dec(n);    end;  for i:=1 to c-1 do    for j:=i+1 to c do      if np_[i]<np_[j] then        begin          tem:=np_[i];          np_[i]:=np_[j];          np_[j]:=tem;        end;  t:=0;  while n<m do    begin      inc(n);      inc(t);      s:=s-np_[t];    end;  writeln(s);end;begin  init;  main;  terminate;end.


 

原创粉丝点击