#include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; int *indexes = new int[n+1]; int num; for (int i=0;i<n;++i) { cin >> num; indexes[num] = i; } long long res = 1; int d = 1; int start = indexes[n], end = indexes[n]; int l; for (int i=n-1;i>=1 && d<n;--i) { if (indexes[i] < start) start = indexes[i]; if (indexes[i] > end) end = indexes[i]; l = end - start + 1; ++d; if (d >= l) { int dpmin = max(start-d+l,0), dpmax = min(start, n-d); res += dpmax - dpmin + 1; } if (d == n) break; ++d; if (d >= l) { int dpmin = max(start-d+l,0), dpmax = min(start, n-d); res += dpmax - dpmin + 1; } } cout << 2*n + 1 << " " << res << endl; delete[] indexes; 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 | #include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; int *indexes = new int[n+1]; int num; for (int i=0;i<n;++i) { cin >> num; indexes[num] = i; } long long res = 1; int d = 1; int start = indexes[n], end = indexes[n]; int l; for (int i=n-1;i>=1 && d<n;--i) { if (indexes[i] < start) start = indexes[i]; if (indexes[i] > end) end = indexes[i]; l = end - start + 1; ++d; if (d >= l) { int dpmin = max(start-d+l,0), dpmax = min(start, n-d); res += dpmax - dpmin + 1; } if (d == n) break; ++d; if (d >= l) { int dpmin = max(start-d+l,0), dpmax = min(start, n-d); res += dpmax - dpmin + 1; } } cout << 2*n + 1 << " " << res << endl; delete[] indexes; return 0; } |