比赛 20101101 评测结果 AAAAATTTTT
题目名称 奇怪的监狱 最终得分 50
用户昵称 ZhouZn1 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-01 21:38:52
显示代码纯文本
program zzn;
var
        p,q,i,j,ans:longint;
        num,peo:array[1..100]of integer;
        v:array[1..1000]of boolean;
procedure init;
begin
        assign(input,'prison.in');
        reset(input);
        assign(output,'prison.out');
        rewrite(output);
        readln(p,q);
        for i:=1 to q do read(peo[i]);
end;
procedure closef;
begin
        close(input);
        close(output);
end;
function work:longint;
var
        s,sum,i,j,x,l:longint;
        v:array[1..1000]of boolean;
begin
        sum:=0;
        fillchar(v,sizeof(v),0);
        for i:=1 to q do
         begin
            x:=peo[num[i]];
            v[x]:=true;
            s:=0;
            for j:=x-1 downto 1 do if v[j] then break else
             begin
                 inc(s);
             end;
            for j:=x+1 to p do if v[j] then break else
             inc(s);

            inc(sum,s);
         end;
         exit(sum);
end;
procedure dep(x:integer);
var
        i:longint;
begin
        if x>q then
         begin
               i:=work;
               if i<ans then ans:=i;
         end;
        for i:=1 to q do
         if not(v[i]) then
          begin
              num[x]:=i;
              v[i]:=true;
              dep(x+1);
              v[i]:=false;
          end;
end;
procedure main;
begin
        ans:=maxlongint;
        fillchar(v,sizeof(v),0);
        dep(1);
        writeln(ans);
end;
begin
        init;
        main;
        closef;
end.