#include <stdio.h> const int max_size = 1e6; #define scan(args...) [&]{ return scanf(args); }(); inline int min(int a, int b, int c = max_size+1) { int minimum = a; if (b < minimum) minimum = b; if (c < minimum) minimum = c; return minimum; } int main() { int n; int a[max_size]; int pos[max_size]; scan("%d", &n); for(int i = 0; i < n; i++) { scan("%d", &a[i]); pos[a[i]-1] = i; } long long solution = 1; int left = pos[n-1]; int right = pos[n-1]; for (int X = 2; X <= n; X++) { if (X % 2 == 0) { int i = n - 1 - (X / 2); if (pos[i] < left) left = pos[i]; if (pos[i] > right) right = pos[i]; } int range_size = right - left + 1; //printf("%d %d\n", X, range_size); if (X >= range_size) { int l = min(left, X-range_size); int r = min(n-right-1, X-range_size); solution += l + r - X + range_size + 1; //printf("%d %d %d\n", l, r, l + r - X - range_size + 1); } } printf("%d %lld\n", 2 * n + 1, solution); 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <stdio.h> const int max_size = 1e6; #define scan(args...) [&]{ return scanf(args); }(); inline int min(int a, int b, int c = max_size+1) { int minimum = a; if (b < minimum) minimum = b; if (c < minimum) minimum = c; return minimum; } int main() { int n; int a[max_size]; int pos[max_size]; scan("%d", &n); for(int i = 0; i < n; i++) { scan("%d", &a[i]); pos[a[i]-1] = i; } long long solution = 1; int left = pos[n-1]; int right = pos[n-1]; for (int X = 2; X <= n; X++) { if (X % 2 == 0) { int i = n - 1 - (X / 2); if (pos[i] < left) left = pos[i]; if (pos[i] > right) right = pos[i]; } int range_size = right - left + 1; //printf("%d %d\n", X, range_size); if (X >= range_size) { int l = min(left, X-range_size); int r = min(n-right-1, X-range_size); solution += l + r - X + range_size + 1; //printf("%d %d %d\n", l, r, l + r - X - range_size + 1); } } printf("%d %lld\n", 2 * n + 1, solution); return 0; } |