#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); REP(i, n) { cin >> a[i]; --a[i]; } vi p(n); REP(i, n) p[a[i]] = i; set<int> ps; long long result = 0; for (int i = n - 1; i >= 0; --i) { ps.insert(p[i]); int mn = *ps.begin(), mx = *ps.rbegin(); int len = 2 * SZ(ps) - 1; if (mx - mn + 1 <= len && len <= n) { int l_min = max(0, mx - len + 1); int l_max = min(mn, n - len); result += l_max - l_min + 1; } len = 2 * SZ(ps) - 2; if (len > 0 && mx - mn + 1 <= len && len <= n) { int l_min = max(0, mx - len + 1); int l_max = min(mn, n - len); result += l_max - l_min + 1; } } cout << 2 * n + 1 << ' ' << result << endl; }
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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); REP(i, n) { cin >> a[i]; --a[i]; } vi p(n); REP(i, n) p[a[i]] = i; set<int> ps; long long result = 0; for (int i = n - 1; i >= 0; --i) { ps.insert(p[i]); int mn = *ps.begin(), mx = *ps.rbegin(); int len = 2 * SZ(ps) - 1; if (mx - mn + 1 <= len && len <= n) { int l_min = max(0, mx - len + 1); int l_max = min(mn, n - len); result += l_max - l_min + 1; } len = 2 * SZ(ps) - 2; if (len > 0 && mx - mn + 1 <= len && len <= n) { int l_min = max(0, mx - len + 1); int l_max = min(mn, n - len); result += l_max - l_min + 1; } } cout << 2 * n + 1 << ' ' << result << endl; } |