#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 1000005; int n, x, poz[N], le, ri, oc, i, bet, fr, fr2; LL cou; int main() { scanf("%d", &n); for (i=1;i<=n;i++) { scanf("%d", &x); poz[x] = i; } cou = 1; le = ri = poz[n]; fr = 0; oc = 1; for (i=2;i<=n;i++) { if (i % 2 == 1) fr++; else { le = min(le, poz[n - i / 2]); ri = max(ri, poz[n - i / 2]); oc++; } bet = ri - le + 1 - oc; if (fr < bet) continue; fr2 = fr - bet; //printf("i = %d, oc = %d, bet = %d, fr = %d, fr2 = %d\n", i, oc, bet, fr, fr2); cou += (fr2 + 1); if (le - fr2 < 1) cou -= (1 - (le - fr2)); if (ri + fr2 > n) cou -= (ri + fr2 - n); //printf("%d %lld\n", i, cou); } printf("%d %lld\n", 2 * n + 1, cou); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 1000005; int n, x, poz[N], le, ri, oc, i, bet, fr, fr2; LL cou; int main() { scanf("%d", &n); for (i=1;i<=n;i++) { scanf("%d", &x); poz[x] = i; } cou = 1; le = ri = poz[n]; fr = 0; oc = 1; for (i=2;i<=n;i++) { if (i % 2 == 1) fr++; else { le = min(le, poz[n - i / 2]); ri = max(ri, poz[n - i / 2]); oc++; } bet = ri - le + 1 - oc; if (fr < bet) continue; fr2 = fr - bet; //printf("i = %d, oc = %d, bet = %d, fr = %d, fr2 = %d\n", i, oc, bet, fr, fr2); cou += (fr2 + 1); if (le - fr2 < 1) cou -= (1 - (le - fr2)); if (ri + fr2 > n) cou -= (ri + fr2 - n); //printf("%d %lld\n", i, cou); } printf("%d %lld\n", 2 * n + 1, cou); return 0; } |