比赛 |
20120709 |
评测结果 |
AAAAAAAAAA |
题目名称 |
磁性链 |
最终得分 |
100 |
用户昵称 |
isabella |
运行时间 |
0.010 s |
代码语言 |
Pascal |
内存使用 |
0.34 MiB |
提交时间 |
2012-07-09 11:45:06 |
显示代码纯文本
var
n,q,i,j,k,ans,temp,s,t,l,len:longint;
a:array[0..150]of longint;
f,w:array[0..150,0..150]of longint;
function min(a,b:longint):longint;
begin if a<b then exit(a) else exit(b);end;
begin
assign(input,'linka.in');reset(input);
assign(output,'linka.out');rewrite(output);
readln(n,q);
j:=0;
for i:=1 to q do read(a[i]);
for i:=1 to q-1 do
for j:=1 to q-i do
if a[j]>a[j+1] then
begin temp:=a[j];a[j]:=a[j+1];a[j+1]:=temp;end;
ans:=0;
if a[1]=1 then begin ans:=ans+a[2]-2;s:=1;a[1]:=0;end
else begin s:=0;a[0]:=0;end;
if a[q]=n then begin ans:=ans+n-1-a[q-1];t:=q;a[q]:=n+1;end
else begin t:=q+1;a[q+1]:=n+1;end;
fillchar(f,sizeof(f),$3f);
len:=t-s;
for i:=s to t-1 do f[i,i+1]:=0;
for l:=2 to len do
for i:=s to t-l do
begin
j:=i+l;
for k:=i+1 to j-1 do
f[i,j]:=min(f[i,j],f[i,k]+f[k,j]+a[j]-a[i]-2);
end;
writeln(ans+f[s,t]);
close(input);close(output);
end.