#include <iostream> using namespace std; typedef long long ll; const int N = 1000000; int a[N]; int pos[N]; int n; int excess(int target, int L, int R) { if (R - L + 1 > target) return 0; if (target > n) return 0; int r1 = max(R, target - 1); int r2 = min(n - 1, L + target - 1); int result = r2 - r1 + 1; //cerr << target << " containing [" << L << ", " << R << "] -> " << result << endl; return result; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; pos[a[i]] = i; } ll ways = 1; int L = pos[n-1]; int R = pos[n-1]; for (int i = 2; i <= n; i++) { L = min(L, pos[n-i]); R = max(R, pos[n-i]); int l = R - L + 1; int l1 = 2 * i - 1; ways += excess(2 * i - 2, L, R); ways += excess(2 * i - 1, L, R); } cout << 2*n + 1 << ' ' << ways << '\n'; }
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 | #include <iostream> using namespace std; typedef long long ll; const int N = 1000000; int a[N]; int pos[N]; int n; int excess(int target, int L, int R) { if (R - L + 1 > target) return 0; if (target > n) return 0; int r1 = max(R, target - 1); int r2 = min(n - 1, L + target - 1); int result = r2 - r1 + 1; //cerr << target << " containing [" << L << ", " << R << "] -> " << result << endl; return result; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; pos[a[i]] = i; } ll ways = 1; int L = pos[n-1]; int R = pos[n-1]; for (int i = 2; i <= n; i++) { L = min(L, pos[n-i]); R = max(R, pos[n-i]); int l = R - L + 1; int l1 = 2 * i - 1; ways += excess(2 * i - 2, L, R); ways += excess(2 * i - 1, L, R); } cout << 2*n + 1 << ' ' << ways << '\n'; } |