#include <iostream> #include <vector> #include <algorithm> using namespace std; int numRanks, seqLen, med, cost, maxCost = -1, numMax = 0; vector<int> ranks; int costBF(int from, int to) { vector<int> tmp; if(from > to) return -1; for(int i=from; i<=to; ++i) tmp.push_back(ranks[i]); sort(tmp.begin(), tmp.end()); if(tmp.size() % 2) return tmp.size() + 2*tmp[tmp.size()/2]; else return tmp.size() + tmp[tmp.size()/2] + tmp[tmp.size()/2 - 1]; } int main() { ios::sync_with_stdio(false); cin >> numRanks; seqLen = numRanks; ranks.resize(numRanks); for(int i=0; i<numRanks; ++i) { cin >> ranks[i]; } for(int i=0; i<numRanks; ++i) { for(int j=numRanks-1; j>=i; --j) { cost = costBF(i, j); if(cost>maxCost) { maxCost = cost; numMax = 1; } else if(cost==maxCost) ++numMax; } } cout << maxCost << " " << numMax << 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int numRanks, seqLen, med, cost, maxCost = -1, numMax = 0; vector<int> ranks; int costBF(int from, int to) { vector<int> tmp; if(from > to) return -1; for(int i=from; i<=to; ++i) tmp.push_back(ranks[i]); sort(tmp.begin(), tmp.end()); if(tmp.size() % 2) return tmp.size() + 2*tmp[tmp.size()/2]; else return tmp.size() + tmp[tmp.size()/2] + tmp[tmp.size()/2 - 1]; } int main() { ios::sync_with_stdio(false); cin >> numRanks; seqLen = numRanks; ranks.resize(numRanks); for(int i=0; i<numRanks; ++i) { cin >> ranks[i]; } for(int i=0; i<numRanks; ++i) { for(int j=numRanks-1; j>=i; --j) { cost = costBF(i, j); if(cost>maxCost) { maxCost = cost; numMax = 1; } else if(cost==maxCost) ++numMax; } } cout << maxCost << " " << numMax << endl; return 0; } |