记录编号 352250 评测结果 AAAAAAAAAA
题目名称 [NOIP 2005]篝火晚会 最终得分 100
用户昵称 GravatarCodeLyoko 是否通过 通过
代码语言 Pascal 运行时间 0.127 s
提交时间 2016-11-16 23:10:53 内存使用 1.13 MiB
显示代码纯文本
var
  n,m,i,j,t:longint;
  a:array[-1..50500,1..2] of longint;
  b:array[-1..50500] of longint;
  c:array[-50500..50500] of longint;

procedure pre; //预处理(应该算是吧),求出座位排法,否则,输出无解。
var
  i,j:longint;
begin
  b[2]:=1;
  b[1]:=a[1,1];
  b[3]:=a[1,2];
  for i:=3 to n-1 do
    if a[b[i],1]=b[i-1] then b[i+1]:=a[b[i],2]
    else if a[b[i],2]=b[i-1] then b[i+1]:=a[b[i],1]
    else begin
      writeln('-1');
      close(output);
      halt;
    end;
  if not((b[1]=a[b[n],1])or(b[1]=a[b[n],2]))then begin
    writeln('-1');
    close(output);
    halt;
  end;
end;

procedure work;//统计最少次数
var
  i,t:longint;
begin
  for i:=1-n to n-1 do
    c[i]:=0;
  for i:=1 to n do begin
    t:=b[i]-i;
    if t<0 then t:=t+n;
    inc(c[t]);
  end;
  for i:=0 to n-1 do
    if (n-c[i])<m then m:=n-c[i];
end;

begin
  assign(input,'fire.in');assign(output,'fire.out');
  reset(input);rewrite(output);
  readln(n);
  for i:=1 to n do
    readln(a[i,1],a[i,2]);
  pre;
  m:=maxlongint;
  work;
  for i:=1 to n div 2 do begin //反转后再算一遍
    t:=b[i];
    b[i]:=b[n-i+1];
    b[n-i+1]:=t;
  end;
  work;
  writeln(m);
  close(input);
  close(output);
end.