ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1252. Sorting the Tombstones

:(( I`m going to get MAD what`s wrong indeed? (+)
Posted by Pasha 19 Mar 2003 14:18
Const
  Maxn                = 130002;
Var
  N, I, X, L, Ans     : LongInt;
  P                   : Array[0..MaxN] of LongInt;

Function GCD(X,Y:LongInt):LongInt;
Begin
  If Y>X Then
    GCD:=GCD(Y,X)
  Else
    If Y=0 Then
      GCD:=X
    Else
      GCD:=GCD(Y, X MOD Y);
end;

begin
  Readln(N);
  For I := 1 To N Do
  Begin
    Read(X);
    P[X]:=I;
  End;
  X:=0;
  Ans:=0;
  For I := 1 To MaxN Do
    If P[I]>0 Then
    Begin
      Inc(X);
      L:=Abs(P[I]-X);
      If L>0 Then
        If Ans>0 Then
          Ans:=GCD(Ans,L)
        Else
          Ans:=L;
    End;
  If Ans=0 Then
    WriteLn(N-1)
  Else
    WriteLn(Ans-1);
end.
Re: :(( I`m going to get MAD what`s wrong indeed? (+)
Posted by hajime 19 Mar 2003 14:39
> Const
>   Maxn                = 130002;
> Var
>   N, I, X, L, Ans     : LongInt;
>   P                   : Array[0..MaxN] of LongInt;
>
> Function GCD(X,Y:LongInt):LongInt;
> Begin
>   If Y>X Then
>     GCD:=GCD(Y,X)
>   Else
>     If Y=0 Then
>       GCD:=X
>     Else
>       GCD:=GCD(Y, X MOD Y);
> end;
>
> begin
>   Readln(N);
>   For I := 1 To N Do
>   Begin
>     Read(X);
>     P[X]:=I;
>   End;
>   X:=0;
>   Ans:=0;
>   For I := 1 To MaxN Do
>     If P[I]>0 Then
>     Begin
>       Inc(X);
>       L:=Abs(P[I]-X);
>       If L>0 Then
>         If Ans>0 Then
>           Ans:=GCD(Ans,L)
>         Else
>           Ans:=L;
>     End;
>   If Ans=0 Then
>     WriteLn(N-1)
>   Else
>     WriteLn(Ans-1);
> end.

Sorting may be also in descending order
Thanks a lot for your help
Posted by Pasha 19 Mar 2003 16:20
My mail address is aidin_n7@hotmai.com
I will glad to meet you more
Best
Aidin
Re: Thanks a lot for your help
Posted by lengyue2005 10 Nov 2005 05:01
Const
  Maxn = 1302;
Var
  N, I, X, L, Ans,x2,l2,ans2: LongInt;
  P : Array[0..MaxN] of LongInt;

Function GCD(X,Y:LongInt):LongInt;
Begin
If Y>X Then
GCD:=GCD(Y,X)
Else
If Y=0 Then
GCD:=X
Else
GCD:=GCD(Y, X MOD Y);
end;

begin
  Readln(N);
  For I := 1 To N Do
    Begin
      Read(X);
      P[X]:=I;
    End;
  X:=0;
  x2:=n;
  Ans:=0;
  ans2:=0;
  For I := 1 To MaxN Do
    If P[I]>0
      Then Begin
             Inc(X);
             dec(x2);
             L:=Abs(P[I]-X);
             l2:=abs(p[i]-x2);
             If L>0
               Then If Ans>0
                      Then Ans:=GCD(Ans,L)
                      Else Ans:=L;
             If L2>0
               Then If Ans2>0
                      Then Ans2:=GCD(Ans2,L2)
                      Else Ans2:=L2;
           End;
  if ans=0
    then ans:=n-1
    else ans:=ans-1;
  if ans2=0
    then ans2:=n-1
    else ans2:=ans2-1;
  if ans>ans2
    then writeln(ans)
    else writeln(ans2);

end.
sorry...
i used some of  your code..