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

what's wrong WA#2
Posted by tutuna 19 Sep 2012 16:19
# include <iostream>
# include <cstdio>

using namespace std;

const int Maxn = 130012;

int n, ans1, ans2, L1, L2, x, p[Maxn], q1, q2;

int GCD (int a, int b){

 if (b > a) return GCD(b, a);
 else
 if (b == 0) return a;
 else
return GCD(b, a % b);

}



int main () {
    freopen ("1.txt", "r", stdin);
    freopen ("2.txt", "w", stdout);

    cin>>n;

    for (int i = 1; i <= n; i++){
     cin>>x;
     p[x] = i;
    }

    q1 = 0;
    q2 = n + 1;
    ans1 = 0;
    ans2 = 0;

    for (int i = 1; i <= Maxn; i++){
        if (p[i] > 0) {
          q1 ++; q2 --;

          L1 = p[i] - q1; if (L1 < 0) L1 = -L1;
          L2 = p[i] - q2; if (L2 < 0) L2 = -L2;

          if (L1 > 0)
              if (ans1 > 0) ans1 = GCD(ans1, L1); else ans1 = L1;

          if (L2 > 0)
              if (ans2 > 0) ans2 = GCD(ans2, L2); else ans2 = L2;



        }

    }

    if (ans1 == 0) ans1 = n - 1; else ans1 --;
    if (ans2 == 0) ans2 = n - 1; else ans2 --;

    if (ans1 > ans2) cout<<ans1; else cout<<ans2;

return 0;
}

Edited by author 19.09.2012 17:23