#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; cin>>n; vector<int> grade(n); for(int i = 0; i < n; i++){ cin>>grade[i]; } int maksiumum = -1, how_many_ways = 0; for(int l = 0; l < n; l++){ for(int r = n-1; r >= l; r--){ vector<int> tmp; vector<int>::iterator it = grade.begin(); tmp.assign(it + l, it + r + 1); //cout<<l<<" "<<r<<endl; //for(auto cur : tmp)cout<<cur<<" ";cout<<endl<<endl; int median = 0, middle, mod_2 = 0; if(tmp.size() % 2 == 0){ middle = (int)tmp.size()/2 - 1; nth_element(tmp.begin(),tmp.begin() + (middle), tmp.end()); median = tmp[middle]; nth_element(tmp.begin(),tmp.begin() + (++middle), tmp.end()); median += tmp[middle]; if(median % 2 == 1) mod_2 = 1; median /= 2; } else{ middle = (int)tmp.size()/2; nth_element(tmp.begin(),tmp.begin() + (middle), tmp.end()); median = tmp[middle]; } //int l_ile = 1, r_ile = 4; //if(l + 1 == l_ile && r + 1 == r_ile) cout<<tmp.size()<<" "<<median<<endl; //cout<<tmp.size() + 2 * median<<" "<<tmp.size()<<endl; if((int)tmp.size() + 2 * median + mod_2 == maksiumum){ how_many_ways++; } else if((int)tmp.size() + 2 * median + mod_2 > maksiumum){ how_many_ways = 1; maksiumum = (int)tmp.size() + 2 * median; } } } cout<<maksiumum<<" "<<how_many_ways<<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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; cin>>n; vector<int> grade(n); for(int i = 0; i < n; i++){ cin>>grade[i]; } int maksiumum = -1, how_many_ways = 0; for(int l = 0; l < n; l++){ for(int r = n-1; r >= l; r--){ vector<int> tmp; vector<int>::iterator it = grade.begin(); tmp.assign(it + l, it + r + 1); //cout<<l<<" "<<r<<endl; //for(auto cur : tmp)cout<<cur<<" ";cout<<endl<<endl; int median = 0, middle, mod_2 = 0; if(tmp.size() % 2 == 0){ middle = (int)tmp.size()/2 - 1; nth_element(tmp.begin(),tmp.begin() + (middle), tmp.end()); median = tmp[middle]; nth_element(tmp.begin(),tmp.begin() + (++middle), tmp.end()); median += tmp[middle]; if(median % 2 == 1) mod_2 = 1; median /= 2; } else{ middle = (int)tmp.size()/2; nth_element(tmp.begin(),tmp.begin() + (middle), tmp.end()); median = tmp[middle]; } //int l_ile = 1, r_ile = 4; //if(l + 1 == l_ile && r + 1 == r_ile) cout<<tmp.size()<<" "<<median<<endl; //cout<<tmp.size() + 2 * median<<" "<<tmp.size()<<endl; if((int)tmp.size() + 2 * median + mod_2 == maksiumum){ how_many_ways++; } else if((int)tmp.size() + 2 * median + mod_2 > maksiumum){ how_many_ways = 1; maksiumum = (int)tmp.size() + 2 * median; } } } cout<<maksiumum<<" "<<how_many_ways<<endl; } |