Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <assert.h>
- #include <unordered_map>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef vector < long long > vll;
- typedef pair < long long, long long > pll;
- typedef pair < int, int > pii;
- typedef vector < int > vii;
- #define csl ios_base::sync_with_stdio(false); cin.tie(NULL)
- #define l(x) (((x) << 1) | 1)
- #define r(x) ((l(x)) + 1)
- #define mp make_pair
- #define fst first
- #define snd second
- ll t, n, u, v, m, q, r, ql, qr, k, l, s, x, y, w;
- const int N = 3e6 + 500;
- const int shift = 1e6 + 50;
- const long long mod = 1e9 + 7;
- const long long INF = 1LL << 57LL;
- const bool JUDGE = false;
- int p[N / 2], revp[N / 2], cnt[N];
- int main(){
- csl;
- if (JUDGE) {
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- }
- cin >> n;
- cin >> p[1];
- revp[1] = p[1];
- for (int i = 0; i < n - 1; ++i) {
- cin >> p[i + 2];
- revp[n - i] = p[i + 2];
- }
- for (int i = 1; i <= n; ++i) {
- ll x = (i - p[i]) % n;
- ll y;
- y = (x > 0) ? x - n : n + x;
- if (abs(x) <= abs(y)) cnt[x + shift]++;
- else cnt[y + shift]++;
- //cout << x << ' ' << y << '\n';
- }
- ll ans = 0;
- ll ct = 0;
- ll jj = 0;
- bool yy = true;
- for (int i = (1 - ceil((double)n / 2.0)) + (int)shift; i <= floor((double)n / 2.0) + shift; ++i) {
- if (!cnt[i]) {
- ct++;
- if (yy) jj++;
- }
- else {
- if (yy) yy = false;
- ans = max(ans, ct);
- ct = 0;
- cnt[i] = 0;
- continue;
- }
- }
- ans = max(ans, ct + jj);
- ct = 0;
- for (int i = 1; i <= n; ++i) {
- ll x = (i - revp[i]) % n;
- ll y;
- y = (x > 0) ? x - n : n + x;
- if (abs(x) <= abs(y)) cnt[x + shift]++;
- else cnt[y + shift]++;
- }
- int j = 0;
- bool kx = true;
- for (int i = (1 - ceil((double)n / 2.0)) + (int)shift; i <= floor((double)n / 2.0) + shift; ++i) {
- if (!cnt[i]) {
- ct++;
- if (kx) j++;
- }
- else {
- if (kx) kx = false;
- ans = max(ans, ct);
- ct = 0;
- cnt[i] = 0;
- continue;
- }
- }
- ans = max(ans, ct + j);
- int i = n / 2;
- j = (int)abs(1 - ceil(double(n) / 2.0));
- if (i == j) cout << i - (ans / 2) << '\n';
- else cout << max(i, j) - ((ans + 1) / 2) << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement