#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int> a(n); for(int &ai : a) { cin >> ai; --ai; } vector<int> pos(n, -1); REP(i, n) pos[a[i]] = i; LL ans = 0; int min_l = n, max_r = -1; for(int val = n - 1; val >= 0; --val) { min_l = min(min_l, pos[val]); max_r = max(max_r, pos[val]); auto consider = [&](int to_add) { debug(val, min_l, max_r, to_add); if(to_add < 0 or to_add > min_l + (n - 1 - max_r)) return; int min_from_left = max(0, to_add - (n - 1 - max_r)); int max_from_left = min(to_add, min_l); debug(min_from_left, max_from_left); ans += max_from_left - min_from_left + 1; }; int to_add = n - val - 1 - ((max_r - min_l + 1) - (n - val)); consider(to_add); if(val != n - 1) consider(to_add - 1); } cout << 2 * n + 1 << ' ' << ans << '\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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int> a(n); for(int &ai : a) { cin >> ai; --ai; } vector<int> pos(n, -1); REP(i, n) pos[a[i]] = i; LL ans = 0; int min_l = n, max_r = -1; for(int val = n - 1; val >= 0; --val) { min_l = min(min_l, pos[val]); max_r = max(max_r, pos[val]); auto consider = [&](int to_add) { debug(val, min_l, max_r, to_add); if(to_add < 0 or to_add > min_l + (n - 1 - max_r)) return; int min_from_left = max(0, to_add - (n - 1 - max_r)); int max_from_left = min(to_add, min_l); debug(min_from_left, max_from_left); ans += max_from_left - min_from_left + 1; }; int to_add = n - val - 1 - ((max_r - min_l + 1) - (n - val)); consider(to_add); if(val != n - 1) consider(to_add - 1); } cout << 2 * n + 1 << ' ' << ans << '\n'; } |