#include <vector> #include <iostream> #include <iomanip> #include <algorithm> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int,int>; using vpii = vector<pii>; using graph = vector<vi>; #define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__) #define all(x) begin(x), end(x) #define mp make_pair #define mt make_tuple template<class T> void initialize_matrix(vector<vector<T>>& matrix, int rows, int cols, T value) { assert(matrix.empty()); FOR (row, rows) matrix.emplace_back(cols, value); } void go() { int n; cin >> n; vi A(n); for (auto &i : A) cin >> i; vi pos(n + 1, 0); FOR (i, n) pos[A[i]] = i; ll ans = 0; int min_pos = 1e9; int max_pos = -1e9; for (int size = 1; size <= n; size++) { int new_element = n - size/2; min_pos = std::min(min_pos, pos[new_element]); max_pos = std::max(max_pos, pos[new_element]); int current_size = max_pos - min_pos + 1; /* cout << "size: " << size << ", median: " << std::setw(5) << n - double(size - 1)/2 << ", A: "; FOR (i, n) { cout << (min_pos == i? '[' : ' '); cout << A[i]; cout << (max_pos == i? ']' : ' '); } cout << " ==> +"; */ if (current_size > size) { // cout << 0 << endl; continue; } else if (current_size == size) { // cout << 1 << endl; ans++; } else { int left_margin = min_pos; int right_margin = n - max_pos - 1; int diff = size - current_size; int add = diff + 1; if (left_margin < diff) add = std::max(0, add - (diff - left_margin)); if (right_margin < diff) add = std::max(0, add - (diff - right_margin)); // cout << add << endl; ans += add; } } cout << 2*n + 1 << " " << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); go(); 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 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <vector> #include <iostream> #include <iomanip> #include <algorithm> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int,int>; using vpii = vector<pii>; using graph = vector<vi>; #define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__) #define all(x) begin(x), end(x) #define mp make_pair #define mt make_tuple template<class T> void initialize_matrix(vector<vector<T>>& matrix, int rows, int cols, T value) { assert(matrix.empty()); FOR (row, rows) matrix.emplace_back(cols, value); } void go() { int n; cin >> n; vi A(n); for (auto &i : A) cin >> i; vi pos(n + 1, 0); FOR (i, n) pos[A[i]] = i; ll ans = 0; int min_pos = 1e9; int max_pos = -1e9; for (int size = 1; size <= n; size++) { int new_element = n - size/2; min_pos = std::min(min_pos, pos[new_element]); max_pos = std::max(max_pos, pos[new_element]); int current_size = max_pos - min_pos + 1; /* cout << "size: " << size << ", median: " << std::setw(5) << n - double(size - 1)/2 << ", A: "; FOR (i, n) { cout << (min_pos == i? '[' : ' '); cout << A[i]; cout << (max_pos == i? ']' : ' '); } cout << " ==> +"; */ if (current_size > size) { // cout << 0 << endl; continue; } else if (current_size == size) { // cout << 1 << endl; ans++; } else { int left_margin = min_pos; int right_margin = n - max_pos - 1; int diff = size - current_size; int add = diff + 1; if (left_margin < diff) add = std::max(0, add - (diff - left_margin)); if (right_margin < diff) add = std::max(0, add - (diff - right_margin)); // cout << add << endl; ans += add; } } cout << 2*n + 1 << " " << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); go(); return 0; } |