1 条题解

  • 0
    @ 2021-6-14 23:29:57

    Pascal :

    var
     a:array[1..50000,1..2] of longint;  
     dao,yuan,ci,cha,bo,l:array[0..50000] of longint;  
     max,i,j,n,t:longint;  
     m:boolean;  
    procedure init;  
    begin  
     readln(n);  
     for i:=1 to n do  
     begin  
      read(a[i,1],a[i,2]);  
      yuan[i]:=i;  
      dao[i]:=n+1-i;  
     end;  
    end;  
    procedure combine;  
    begin  
     l[1]:=1;  
     j:=1;  
     bo[1]:=1;  
     repeat  
      if j<>1 then  
       if (a[l[j],1]<>l[j-1])and(a[l[j],2]<>l[j-1]) then  
       begin  
        writeln(-1);  
        halt;  
       end;  
      for t:=1 to 2 do  
       if bo[a[l[j],t]]=0 then  
       begin  
        bo[a[l[j],t]]:=1;  
        inc(j);  
        l[j]:=a[l[j-1],t];  
        break;  
       end;  
     until j=n;  
     m:=true;  
     if(a[l[n],1]=l[n-1])and(a[l[n],2]=l[1]) then m:=false;  
     if(a[l[n],2]=l[n-1])and(a[l[n],1]=l[1]) then m:=false;  
     if m then  
     begin  
      writeln(-1);  
      halt;  
     end;  
    end;  
    procedure judge;  
    begin  
     max:=0;  
     for i:=1 to n do  
     begin  
      cha[i]:=yuan[l[i]]-i;  
     if cha[i]<0 then cha[i]:=cha[i]+n;  
      inc(ci[cha[i]]);  
    end;  
     for i:=0 to n do  
      if ci[i]>max then max:=ci[i];  
     fillchar(ci,sizeof(ci),0);  
     for i:=1 to n do  
     begin  
      cha[i]:=dao[l[i]]-i;  
      if cha[i]<0 then cha[i]:=cha[i]+n;  
      inc(ci[cha[i]]);  
     end;  
     for i:=0 to n do  
      if ci[i]>max then max:=ci[i];  
    end;  
    begin  
     init;  
     combine;  
     judge;  
     writeln(n-max);  
    end.
    
    • 1

    信息

    ID
    244
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者