#include <iostream> #include <algorithm> #include <math.h> #include <vector> #include <time.h> #include <list> using namespace std; long fcelu(long n, list<long> a) { if (n == 1) return 2 * a.front() + n; if (n == 2) return a.front() + a.back() + n; list<long> l = a; l.sort(); vector<long> v; for (long i = 0; i < (n+1)/2+1; i++) { v.push_back(l.front()); l.pop_front(); } if (n % 2 == 0) return v[n/2-1]+v[n/2]+n; else return 2* v[(n+1)/2-1]+n; } vector<long> maximizefcelu(long n, list<long> a) { list<long> l = a; long fc; long max = 0; long ile = 0; for (long i = 0; i < n; i++) { for (long j = n; j > i; j--) { l = a; for (long k = 0; k <= i; k++) if(!l.empty()&&k!=0) l.pop_front(); for (long k = n; k >= j; k--) if (!l.empty()&&k!=n) l.pop_back(); fc= fcelu(j - i, l); if (fc == max) ile++; if (fc > max) { max = fc; ile = 1; } } } return vector<long>{max, ile}; } int main() { ios_base::sync_with_stdio(false); long n, ai; cin >> n; list<long> a; for (long i = 0; i < n; i++) { cin >> ai; a.push_back(ai); } cout << maximizefcelu(n, a)[0] << " " << maximizefcelu(n, a)[1] << endl; 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 | #include <iostream> #include <algorithm> #include <math.h> #include <vector> #include <time.h> #include <list> using namespace std; long fcelu(long n, list<long> a) { if (n == 1) return 2 * a.front() + n; if (n == 2) return a.front() + a.back() + n; list<long> l = a; l.sort(); vector<long> v; for (long i = 0; i < (n+1)/2+1; i++) { v.push_back(l.front()); l.pop_front(); } if (n % 2 == 0) return v[n/2-1]+v[n/2]+n; else return 2* v[(n+1)/2-1]+n; } vector<long> maximizefcelu(long n, list<long> a) { list<long> l = a; long fc; long max = 0; long ile = 0; for (long i = 0; i < n; i++) { for (long j = n; j > i; j--) { l = a; for (long k = 0; k <= i; k++) if(!l.empty()&&k!=0) l.pop_front(); for (long k = n; k >= j; k--) if (!l.empty()&&k!=n) l.pop_back(); fc= fcelu(j - i, l); if (fc == max) ile++; if (fc > max) { max = fc; ile = 1; } } } return vector<long>{max, ile}; } int main() { ios_base::sync_with_stdio(false); long n, ai; cin >> n; list<long> a; for (long i = 0; i < n; i++) { cin >> ai; a.push_back(ai); } cout << maximizefcelu(n, a)[0] << " " << maximizefcelu(n, a)[1] << endl; return 0; } |