#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; //std::cin >> Z; while (Z--) { int n; std::cin >> n; vi V(n); for (auto &a : V) std::cin >> a; vl F(n); for (int i = 0; i < n; i++) F[V[i] - 1] = i; ll W = 1; std::vector<pll> RL(n + 1); RL.back() = pll{n + 1, -1}; for (int i = n - 1; i >= 0; i--) RL[i] = {std::min(RL[i + 1].first, F[i]), std::max(RL[i + 1].second, F[i])}; for (int i = 1; i <= n - 1; i++) { if (2 * n - 2 * i <= n && RL[i - 1].second - RL[i - 1].first + 1 <= 2 * n - 2 * i) { ll t1 = RL[i - 1].first, t2 = RL[i - 1].second, miss = 2 * (n - i) - 1 - t2 + t1; t2 += std::max(ll(0), miss - t1); miss -= std::max(ll(0), miss - t1); t1 -= std::max(ll(0), miss - (n - 1 - t2)); miss -= std::max(ll(0), miss - (n - 1 - t2)); W += miss + 1; } if (2 * n + 1 - 2 * i <= n && RL[i - 1].second - RL[i - 1].first <= 2 * n - 2 * i) { ll t1 = RL[i - 1].first, t2 = RL[i - 1].second, miss = 2 * (n - i) - t2 + t1; t2 += std::max(ll(0), miss - t1); miss -= std::max(ll(0), miss - t1); t1 -= std::max(ll(0), miss - (n - 1 - t2)); miss -= std::max(ll(0), miss - (n - 1 - t2)); W += miss + 1; } } std::cout << 2 * n + 1 << ' ' << W << std::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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; //std::cin >> Z; while (Z--) { int n; std::cin >> n; vi V(n); for (auto &a : V) std::cin >> a; vl F(n); for (int i = 0; i < n; i++) F[V[i] - 1] = i; ll W = 1; std::vector<pll> RL(n + 1); RL.back() = pll{n + 1, -1}; for (int i = n - 1; i >= 0; i--) RL[i] = {std::min(RL[i + 1].first, F[i]), std::max(RL[i + 1].second, F[i])}; for (int i = 1; i <= n - 1; i++) { if (2 * n - 2 * i <= n && RL[i - 1].second - RL[i - 1].first + 1 <= 2 * n - 2 * i) { ll t1 = RL[i - 1].first, t2 = RL[i - 1].second, miss = 2 * (n - i) - 1 - t2 + t1; t2 += std::max(ll(0), miss - t1); miss -= std::max(ll(0), miss - t1); t1 -= std::max(ll(0), miss - (n - 1 - t2)); miss -= std::max(ll(0), miss - (n - 1 - t2)); W += miss + 1; } if (2 * n + 1 - 2 * i <= n && RL[i - 1].second - RL[i - 1].first <= 2 * n - 2 * i) { ll t1 = RL[i - 1].first, t2 = RL[i - 1].second, miss = 2 * (n - i) - t2 + t1; t2 += std::max(ll(0), miss - t1); miss -= std::max(ll(0), miss - t1); t1 -= std::max(ll(0), miss - (n - 1 - t2)); miss -= std::max(ll(0), miss - (n - 1 - t2)); W += miss + 1; } } std::cout << 2 * n + 1 << ' ' << W << std::endl; } } |