#include <bits/stdc++.h> using namespace std; template<typename T> T median(vector<T> xs) { sort(xs.begin(), xs.end()); if(xs.size() % 2) return 2 * xs[xs.size() / 2]; return xs[xs.size() / 2 - 1] + xs[xs.size() / 2]; } pair<size_t, int64_t> brute(vector<size_t> pi) { const size_t n = pi.size(); size_t best = 0; int64_t count = 0; for(size_t l = 0; l < n; l++) for(size_t r = l + 1; r <= n; r++) { auto score = (r - l) + median(vector<size_t>(pi.begin() + l, pi.begin() + r)); if(score > best) best = score, count = 0; if(score == best) count++; } return {best, count}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); size_t n; cin >> n; vector<size_t> pi(n); for(size_t i = 0; i < n; i++) cin >> pi[i]; auto [best, cnt] = brute(pi); cout << best << ' ' << cnt << endl; /* vector<size_t> A(n), B(n); for(size_t i = 0; i < n; i++) A[i] = pi[i] - 1, B[A[i]] = i, cout << A[i] << ' '; cout << endl; int64_t result = 0; for(size_t x = n, a = SIZE_MAX, b = 0; x --> 0; ) { a = min(a, B[x]); b = max(b, B[x]); if(x and a <= B[x-1] and B[x-1] <= b) continue; size_t over = int((n - x) / 2) - int((b - a + 1) - (n - x)); // 0 <= i <= a // 0 <= j <= n - 1 - b // i + j = over // over - a <= over - i < over // max(1, over - a) <= j < min(n - b, over) cout << x << ":" << n << " -> " << a << ".." << b << "::" << over << endl; for(size_t i = 0; i <= a; i++) for(size_t j = 0; j <= n-1-b; j++) { if(i + j <= over) cout << i << "+" << j << endl, result++; } // auto d = max((int64_t)0, (int64_t)min(n - b, over) - (int64_t)max((size_t)1, over - a)); // result += d; } 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; template<typename T> T median(vector<T> xs) { sort(xs.begin(), xs.end()); if(xs.size() % 2) return 2 * xs[xs.size() / 2]; return xs[xs.size() / 2 - 1] + xs[xs.size() / 2]; } pair<size_t, int64_t> brute(vector<size_t> pi) { const size_t n = pi.size(); size_t best = 0; int64_t count = 0; for(size_t l = 0; l < n; l++) for(size_t r = l + 1; r <= n; r++) { auto score = (r - l) + median(vector<size_t>(pi.begin() + l, pi.begin() + r)); if(score > best) best = score, count = 0; if(score == best) count++; } return {best, count}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); size_t n; cin >> n; vector<size_t> pi(n); for(size_t i = 0; i < n; i++) cin >> pi[i]; auto [best, cnt] = brute(pi); cout << best << ' ' << cnt << endl; /* vector<size_t> A(n), B(n); for(size_t i = 0; i < n; i++) A[i] = pi[i] - 1, B[A[i]] = i, cout << A[i] << ' '; cout << endl; int64_t result = 0; for(size_t x = n, a = SIZE_MAX, b = 0; x --> 0; ) { a = min(a, B[x]); b = max(b, B[x]); if(x and a <= B[x-1] and B[x-1] <= b) continue; size_t over = int((n - x) / 2) - int((b - a + 1) - (n - x)); // 0 <= i <= a // 0 <= j <= n - 1 - b // i + j = over // over - a <= over - i < over // max(1, over - a) <= j < min(n - b, over) cout << x << ":" << n << " -> " << a << ".." << b << "::" << over << endl; for(size_t i = 0; i <= a; i++) for(size_t j = 0; j <= n-1-b; j++) { if(i + j <= over) cout << i << "+" << j << endl, result++; } // auto d = max((int64_t)0, (int64_t)min(n - b, over) - (int64_t)max((size_t)1, over - a)); // result += d; } cout << 2*n+1 << ' ' << result << endl; */ } |