Advertisement
Guest User

Untitled

a guest
Aug 9th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. #include <assert.h>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef long double ld;
  9. typedef vector < long long > vll;
  10. typedef pair < long long, long long > pll;
  11. typedef pair < int, int > pii;
  12. typedef vector < int > vii;
  13.  
  14. #define csl ios_base::sync_with_stdio(false); cin.tie(NULL)
  15. #define l(x) (((x) << 1) | 1)
  16. #define r(x) ((l(x)) + 1)
  17. #define mp make_pair
  18. #define fst first
  19. #define snd second
  20.  
  21. ll t, n, u, v, m, q, r, ql, qr, k, l, s, x, y, w;
  22. const int N = 3e6 + 500;
  23. const int shift = 1e6 + 50;
  24. const long long mod = 1e9 + 7;
  25. const long long INF = 1LL << 57LL;
  26. const bool JUDGE = false;
  27. int p[N / 2], revp[N / 2], cnt[N];
  28. int main(){
  29.     csl;
  30.     if (JUDGE) {
  31.         freopen("in.txt", "r", stdin);
  32.         freopen("out.txt", "w", stdout);
  33.     }
  34.     cin >> n;
  35.     cin >> p[1];
  36.     revp[1] = p[1];
  37.     for (int i = 0; i < n - 1; ++i) {
  38.         cin >> p[i + 2];
  39.         revp[n - i] = p[i + 2];
  40.     }
  41.     for (int i = 1; i <= n; ++i) {
  42.         ll x = (i - p[i]) % n;
  43.         ll y;
  44.         y = (x > 0) ? x - n : n + x;
  45.         if (abs(x) <= abs(y)) cnt[x + shift]++;
  46.         else cnt[y + shift]++;
  47.         //cout << x << ' ' << y << '\n';
  48.     }
  49.     ll ans = 0;
  50.     ll ct = 0;
  51.     ll jj = 0;
  52.     bool yy = true;
  53.     for (int i = (1 - ceil((double)n / 2.0)) + (int)shift; i <= floor((double)n / 2.0) + shift; ++i) {
  54.         if (!cnt[i]) {
  55.             ct++;
  56.             if (yy) jj++;
  57.         }
  58.         else {
  59.             if (yy) yy = false;
  60.             ans = max(ans, ct);
  61.             ct = 0;
  62.             cnt[i] = 0;
  63.             continue;
  64.         }
  65.     }
  66.     ans = max(ans, ct + jj);
  67.     ct = 0;
  68.     for (int i = 1; i <= n; ++i) {
  69.         ll x = (i - revp[i]) % n;
  70.         ll y;
  71.         y = (x > 0) ? x - n : n + x;
  72.         if (abs(x) <= abs(y)) cnt[x + shift]++;
  73.         else cnt[y + shift]++;
  74.     }
  75.     int j = 0;
  76.     bool kx = true;
  77.     for (int i = (1 - ceil((double)n / 2.0)) + (int)shift; i <= floor((double)n / 2.0) + shift; ++i) {
  78.         if (!cnt[i]) {
  79.             ct++;
  80.             if (kx) j++;
  81.         }
  82.         else {
  83.             if (kx) kx = false;
  84.             ans = max(ans, ct);
  85.             ct = 0;
  86.             cnt[i] = 0;
  87.             continue;
  88.         }
  89.     }
  90.     ans = max(ans, ct + j);
  91.     int i = n / 2;
  92.     j = (int)abs(1 - ceil(double(n) / 2.0));
  93.     if (i == j) cout << i - (ans / 2) << '\n';
  94.     else cout << max(i, j) - ((ans + 1) / 2) << '\n';
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement