#include <stdio.h> #include <stdlib.h> //#include <time.h> int main() { //clock_t tic = clock(); int n; scanf("%d", &n); int arr[n]; int positions[n+1]; unsigned ways = 2, last = 0; int maxi = 2*n; for(int i=0;i<n;i++) { scanf("%d", arr + i); positions[arr[i]] = i; if(last) { if (last + arr[i] + 2 == maxi + 1) ways++; } last = arr[i]; } if(n==1) { printf("%u 1", 2*n+1); return 0; } if(n == 2) ways--; for(int i=3;i<n;i++) { int fn = maxi - i + 1; if(i%2) { fn /= 2; int pos = positions[fn]; int si = pos - i + 1, ei = pos + i - 1; if(si < 0) si = 0; if(ei >= n) ei = n-1; int l = 0, g = 0; int j; for(j = si;j<si+i;j++) { if(arr[j] == fn) continue; if(arr[j] < fn) l++; else g++; } if(l == g) { ways++; //printf("%d %d %d\n", fn, si, i); } while(j <= ei) { if(arr[j-i] < fn) l--; else g--; if(arr[j] < fn) l++; else g++; if(l == g) { ways++; //printf("%d %d %d\n", fn, j-i+1, i); } j++; } } else { int st = fn - n; for(int m = st;m<=fn/2;m++) { if(m == fn-m) continue; int n1 = m, n2 = fn - m; int p1 = positions[n1], p2 = positions[n2]; if(p2<p1) { int tmp = p1; p1 = p2; p2 = tmp; } int dist = p2 - p1; if(dist > i) continue; //else printf("%d: KAN: %d %d %d\n", i, n1, n2, dist); int si = p2 - i + 1, ei = p1 + i - 1; if(si < 0) si = 0; if(ei >= n) ei = n-1; int j, l = 0 , g = 0; for(j = si;j<si+i;j++) { if(arr[j] == n1 || arr[j] == n2) continue; if(arr[j] < n1) l++; else g++; } if(l == g) { ways++; //printf("%d %d %d\n", fn, si, i); } while(j <= ei) { if(arr[j-i] < fn) l--; else g--; if(arr[j] < fn) l++; else g++; if(l == g) { ways++; //printf("v2: %d %d %d\n", fn, j-i+1, i); } j++; } } } } printf("%u %u\n", 2*n+1, ways); 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <stdio.h> #include <stdlib.h> //#include <time.h> int main() { //clock_t tic = clock(); int n; scanf("%d", &n); int arr[n]; int positions[n+1]; unsigned ways = 2, last = 0; int maxi = 2*n; for(int i=0;i<n;i++) { scanf("%d", arr + i); positions[arr[i]] = i; if(last) { if (last + arr[i] + 2 == maxi + 1) ways++; } last = arr[i]; } if(n==1) { printf("%u 1", 2*n+1); return 0; } if(n == 2) ways--; for(int i=3;i<n;i++) { int fn = maxi - i + 1; if(i%2) { fn /= 2; int pos = positions[fn]; int si = pos - i + 1, ei = pos + i - 1; if(si < 0) si = 0; if(ei >= n) ei = n-1; int l = 0, g = 0; int j; for(j = si;j<si+i;j++) { if(arr[j] == fn) continue; if(arr[j] < fn) l++; else g++; } if(l == g) { ways++; //printf("%d %d %d\n", fn, si, i); } while(j <= ei) { if(arr[j-i] < fn) l--; else g--; if(arr[j] < fn) l++; else g++; if(l == g) { ways++; //printf("%d %d %d\n", fn, j-i+1, i); } j++; } } else { int st = fn - n; for(int m = st;m<=fn/2;m++) { if(m == fn-m) continue; int n1 = m, n2 = fn - m; int p1 = positions[n1], p2 = positions[n2]; if(p2<p1) { int tmp = p1; p1 = p2; p2 = tmp; } int dist = p2 - p1; if(dist > i) continue; //else printf("%d: KAN: %d %d %d\n", i, n1, n2, dist); int si = p2 - i + 1, ei = p1 + i - 1; if(si < 0) si = 0; if(ei >= n) ei = n-1; int j, l = 0 , g = 0; for(j = si;j<si+i;j++) { if(arr[j] == n1 || arr[j] == n2) continue; if(arr[j] < n1) l++; else g++; } if(l == g) { ways++; //printf("%d %d %d\n", fn, si, i); } while(j <= ei) { if(arr[j-i] < fn) l--; else g--; if(arr[j] < fn) l++; else g++; if(l == g) { ways++; //printf("v2: %d %d %d\n", fn, j-i+1, i); } j++; } } } } printf("%u %u\n", 2*n+1, ways); return 0; } |