#include<cstdio> #include<algorithm> using namespace std; int permutations[1000007]; int numbers[1000007]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &numbers[i]); permutations[numbers[i]] = i; } int maxval = 1 + 2 * n; long long howmany = 1; int smaller = 0; int left = 1, right = n; int pos; int val; left = permutations[n]; right = permutations[n]; for(int dl = 2; dl <= n; dl++) { val = permutations[n-(dl)/2]; if(val < left) left = val; if(val > right) right = val; smaller = right-left+1; if(smaller <= dl) { int left_start = max(1,right-dl+1); int left_end = min(left,n-dl+1); //printf("%d %d %d %d %d %d\n", dl, left_start, left_end, left, right, val); howmany += (left_end - left_start + 1); } } printf("%d %lld", 1 + 2*n, howmany); }
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 | #include<cstdio> #include<algorithm> using namespace std; int permutations[1000007]; int numbers[1000007]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &numbers[i]); permutations[numbers[i]] = i; } int maxval = 1 + 2 * n; long long howmany = 1; int smaller = 0; int left = 1, right = n; int pos; int val; left = permutations[n]; right = permutations[n]; for(int dl = 2; dl <= n; dl++) { val = permutations[n-(dl)/2]; if(val < left) left = val; if(val > right) right = val; smaller = right-left+1; if(smaller <= dl) { int left_start = max(1,right-dl+1); int left_end = min(left,n-dl+1); //printf("%d %d %d %d %d %d\n", dl, left_start, left_end, left, right, val); howmany += (left_end - left_start + 1); } } printf("%d %lld", 1 + 2*n, howmany); } |