比赛 20101101 评测结果 AAAAATTTTT
题目名称 奇怪的监狱 最终得分 50
用户昵称 FrCsKOH 运行时间 5.001 s
代码语言 Pascal 内存使用 0.17 MiB
提交时间 2012-11-05 10:39:29
显示代码纯文本
program C0488;
 var p,q,i,min:longint;
     S:array[1..100] of longint;
     v:array[1..100] of boolean;
     g:array[1..1000] of boolean;

 procedure Search(top,ans:longint);
  var i,j,temp:longint;
  begin
   if top>q then begin
    if ans<min then min:=ans;
   end
   else
    temp:=ans;
    for i:=1 to q do
     if v[i] then begin
      v[i]:=false;
      for j:=S[i]-1 downto 1 do
       if g[j] then inc(ans) else break;
      for j:=S[i]+1 to p do
       if g[j] then inc(ans) else break;
      g[S[i]]:=false;

      Search(top+1,ans);

      g[S[i]]:=true;
      ans:=temp;
      v[i]:=true;
     end;
  end;

 begin
  assign(input,'prison.in');
  reset(input);
  assign(output,'prison.out');
  rewrite(output);

  readln(p,q);
  for i:=1 to q do read(S[i]);
  readln;

  min:=maxlongint;
  fillchar(v,sizeof(v),true);
  fillchar(g,sizeof(g),true);

  Search(1,0);
  writeln(min);

  close(input);
  close(output);
 end.