#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; // a segment of length k is good iff elements from its largest ceil(k/2) elements are the largest elements in a int main() { int t; //scanf("%d", &t); t = 1; while (t--) { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } vector<int> pos(n+1); for (int i = 0; i < n; ++i) { pos[a[i]] = i; } ll res = 0; int l = n+1; int r = -1; for (int m = n, len = 1; len <= n; ++len) { l = min(l, pos[m]); r = max(r, pos[m]); int span = r-l+1; //cout << l << " " << r << " " << len-span << endl; if (span <= len) { int mi = min(l, n-1-r); int ma = max(l, n-1-r); int req = len-span; // 0 <= req <= mi+ma int here = 0; if (req > ma) { req -= ma; mi -= req; here += mi+1; } else { here += min(req, mi)+1; } //cout << here << endl; res += here; } if (len % 2 == 1) { --m; } } printf("%d %lld\n", 2*n+1, res); } 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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; // a segment of length k is good iff elements from its largest ceil(k/2) elements are the largest elements in a int main() { int t; //scanf("%d", &t); t = 1; while (t--) { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } vector<int> pos(n+1); for (int i = 0; i < n; ++i) { pos[a[i]] = i; } ll res = 0; int l = n+1; int r = -1; for (int m = n, len = 1; len <= n; ++len) { l = min(l, pos[m]); r = max(r, pos[m]); int span = r-l+1; //cout << l << " " << r << " " << len-span << endl; if (span <= len) { int mi = min(l, n-1-r); int ma = max(l, n-1-r); int req = len-span; // 0 <= req <= mi+ma int here = 0; if (req > ma) { req -= ma; mi -= req; here += mi+1; } else { here += min(req, mi)+1; } //cout << here << endl; res += here; } if (len % 2 == 1) { --m; } } printf("%d %lld\n", 2*n+1, res); } return 0; } |