#include <bits/stdc++.h> using namespace std; struct TestCase { size_t size; vector<size_t> values; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase tc; cin >> tc.size; tc.values.resize(tc.size); for (auto& value : tc.values) cin >> value; return tc; } void solve_test_case(const TestCase& tc) { vector<size_t> index(tc.size + 1); for (size_t i = 0; i < tc.values.size(); i++) index[tc.values[i]] = i; vector<pair<size_t, size_t>> range_of_suffix(tc.size + 1); range_of_suffix[tc.values.size()] = { index[tc.size], index[tc.size], }; for (size_t i = tc.size - 1; i > 0; i--) { range_of_suffix[i] = { min(index[i], range_of_suffix[i + 1].first), max(index[i], range_of_suffix[i + 1].second), }; } size_t sum = 2 * tc.size + 1; size_t result = 0; for (size_t size = 1; size <= tc.size; size++) { size_t suffix = (sum - size) / 2; auto [left, right] = range_of_suffix[suffix]; int missing = size - (right - left + 1); if (missing < 0) continue; int size_left = left; int size_right = tc.size - right - 1; result += min(missing, size_right) - max(0, missing - size_left) + 1; } cout << sum << " " << result << "\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 56 57 58 59 60 61 62 63 64 65 66 | #include <bits/stdc++.h> using namespace std; struct TestCase { size_t size; vector<size_t> values; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase tc; cin >> tc.size; tc.values.resize(tc.size); for (auto& value : tc.values) cin >> value; return tc; } void solve_test_case(const TestCase& tc) { vector<size_t> index(tc.size + 1); for (size_t i = 0; i < tc.values.size(); i++) index[tc.values[i]] = i; vector<pair<size_t, size_t>> range_of_suffix(tc.size + 1); range_of_suffix[tc.values.size()] = { index[tc.size], index[tc.size], }; for (size_t i = tc.size - 1; i > 0; i--) { range_of_suffix[i] = { min(index[i], range_of_suffix[i + 1].first), max(index[i], range_of_suffix[i + 1].second), }; } size_t sum = 2 * tc.size + 1; size_t result = 0; for (size_t size = 1; size <= tc.size; size++) { size_t suffix = (sum - size) / 2; auto [left, right] = range_of_suffix[suffix]; int missing = size - (right - left + 1); if (missing < 0) continue; int size_left = left; int size_right = tc.size - right - 1; result += min(missing, size_right) - max(0, missing - size_left) + 1; } cout << sum << " " << result << "\n"; } |