#include <bits/stdc++.h> #include <iostream> using namespace std; int n; int pos[1000002]; int elem; long long score = 0; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> elem; pos[elem] = i; } int l = pos[n]; int r = pos[n]; score = 1; for (int i = n - 1; i >= n/2; i--) { int curr = pos[i]; l = min(pos[i], l); r = max(pos[i], r); int len = r - l + 1; int inter = 2*(n - i); if (inter > n) break; if (len <= inter) { int first = max(1, r - inter + 1); int last = min(l, n - inter + 1); score += last - first + 1; } inter++; if (inter > n) break; if (len <= inter) { int first = max(1, r - inter + 1); int last = min(l, n - inter + 1); score += last - first + 1; } //cout << i << ": " << score << endl; } cout << 2*n + 1 << " " << score << endl; 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 | #include <bits/stdc++.h> #include <iostream> using namespace std; int n; int pos[1000002]; int elem; long long score = 0; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> elem; pos[elem] = i; } int l = pos[n]; int r = pos[n]; score = 1; for (int i = n - 1; i >= n/2; i--) { int curr = pos[i]; l = min(pos[i], l); r = max(pos[i], r); int len = r - l + 1; int inter = 2*(n - i); if (inter > n) break; if (len <= inter) { int first = max(1, r - inter + 1); int last = min(l, n - inter + 1); score += last - first + 1; } inter++; if (inter > n) break; if (len <= inter) { int first = max(1, r - inter + 1); int last = min(l, n - inter + 1); score += last - first + 1; } //cout << i << ": " << score << endl; } cout << 2*n + 1 << " " << score << endl; return 0; } |