题目 先用一块木板,把全部的牛棚都围上,然后把最大的空缺(没牛的地方)拆掉,如此执行n-1次,计算出最后剩下的总长度。 { ID:asdcyt01 PROB:barn1 LANG:PASCAL } program barn1(input,output); var a:array[1..200]of boolean; b:array[1..200]of longint; n,m,c,k,p,i:longint; aa,bb:boolean; procedure sort(l,r:longint); var i,j,m,t:longint; begin i:=l;j:=r;m:=b[(i+j)shr 1]; repeat while b[i]>m do inc(i); while b[j]<m do dec(j); if i<=j then begin t:=b[i]; b[i]:=b[j]; b[j]:=t; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; begin assign(input,'barn1.in');reset(input); assign(output,'barn1.out');rewrite(output); readln(n,m,c); fillchar(a,sizeof(a),false); for i:=1 to c do begin readln(p); a[p]:=true; end; k:=0;p:=0; if not a[1] then aa:=true; if not a[m] then bb:=true; for i:=1 to m do begin if a[i]=false then inc(k) else if not a[i-1] then begin inc(p);b[p]:=k;k:=0;end; end; if not a[i-1] then begin inc(p);b[p]:=k;k:=0;end; if aa then begin dec(m,b[1]);b[1]:=0;end; if bb then begin dec(m,b[p]);b[p]:=0;end; sort(1,p); for i:=1 to n-1 do begin if i<=p then dec(m,b[i])else break; end; writeln(m); close(input);close(output); end.