题目: | 修理牛棚 | |
来源: | Usaco1.3.2 | |
题目大意: | 用M节木板覆盖S个牛棚中的C个,求所用木板和最短为多少 | |
数据范围: | 1<= M<=50、1<=C<= S<=200 | |
样例: |
4 50 18 3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43 |
25 |
做题思路: | 把空当排序,找出M-1个最大空当,然后用总长减酷空当和就好 | |
知识点: | 排序,贪心 |
{
ID:Dount Nameless
TASK:barn1
LANG:PASCAL
}
type
act=record
x,d:longint;
end;
var
a:array[0..200]of longint;
t:array[0..200]of act;
n,i,j,s,c,ans:longint;
procedure qsort(l,r:longint);{<快排不解释>}
var
i,j,k:longint;
p:act;
begin
i:=l;j:=r;
k:=t[(l+r)div 2].d;
repeat
whilet[i].d>k do inc(i);
while t[j].d<k do dec(j);
ifi<=j then
begin
p:=t[i];t[i]:=t[j];t[j]:=p;
inc(i);dec(j);
end;
until i>j;
ifi<r then qsort(i,r);
ifj>l then qsort(l,j);
end;
procedure qsorta(l,r:longint);
var
i,j,k,p:longint;
begin
i:=l;j:=r;
k:=a[(l+r)div 2];
repeat
while a[i]<k do inc(i);
while a[j]>k do dec(j);
ifi<=j then
begin
p:=a[i];a[i]:=a[j];a[j]:=p;
inc(i);dec(j);
end;
until i>j;
ifi<r then qsorta(i,r);
ifj>l then qsorta(l,j);
end;
begin
assign(input,'barn1.in');reset(input);
assign(output,'barn1.out');rewrite(output);
fillchar(a,sizeof(a),0);
readln(n,s,c);
fori:=1 to c do
readln(a[i]);
qsorta(1,c);{<排牛棚>}
fori:=1 to c do{<求每段空当>}
begin
t[i].d:=a[i]-a[i-1]-1;
t[i].x:=a[i];
end;
t[1].d:=0;
qsort(1,c); {<排空当>}
ans:=0;
fori:=1 to n-1 do
begin
ans:=ans+t[i].d;
end;
writeln(a[c]-a[1]+1-ans);
close(input);close(output);
end.
题目来源:
http://ace.delos.com/usacoprob2?a=RexmW8CclSt&S=barn1