#include <iostream> #include <vector> #include <unordered_map> using namespace std; vector<int> findMostFrequent(const vector<int>& sequence, unordered_map<int, int>& freqMap) { int maxFreq = 0; vector<int> mostFrequent; for (int num : sequence) { freqMap[num]++; if (freqMap[num] > maxFreq) { maxFreq = freqMap[num]; mostFrequent.clear(); mostFrequent.push_back(num); } else if (freqMap[num] == maxFreq) { mostFrequent.push_back(num); } } return mostFrequent; } vector<vector<int>> divideSequence(const vector<int>& sequence) { unordered_map<int, int> freqMap; vector<int> mostFrequent = findMostFrequent(sequence, freqMap); vector<vector<int>> dividedSequences; if (mostFrequent.size() == sequence.size()) { dividedSequences.push_back(sequence); } else { vector<int> subsequence; for (int num : sequence) { if (freqMap[num] == freqMap[mostFrequent[0]]) { subsequence.push_back(num); } else { dividedSequences.push_back(subsequence); subsequence.clear(); subsequence.push_back(num); } } dividedSequences.push_back(subsequence); } return dividedSequences; } int main() { int length; cin >> length; vector<int> sequence(length); for (int i = 0; i < length; ++i) { cin >> sequence[i]; } vector<vector<int>> dividedSequences = divideSequence(sequence); cout << dividedSequences.size() << 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 | #include <iostream> #include <vector> #include <unordered_map> using namespace std; vector<int> findMostFrequent(const vector<int>& sequence, unordered_map<int, int>& freqMap) { int maxFreq = 0; vector<int> mostFrequent; for (int num : sequence) { freqMap[num]++; if (freqMap[num] > maxFreq) { maxFreq = freqMap[num]; mostFrequent.clear(); mostFrequent.push_back(num); } else if (freqMap[num] == maxFreq) { mostFrequent.push_back(num); } } return mostFrequent; } vector<vector<int>> divideSequence(const vector<int>& sequence) { unordered_map<int, int> freqMap; vector<int> mostFrequent = findMostFrequent(sequence, freqMap); vector<vector<int>> dividedSequences; if (mostFrequent.size() == sequence.size()) { dividedSequences.push_back(sequence); } else { vector<int> subsequence; for (int num : sequence) { if (freqMap[num] == freqMap[mostFrequent[0]]) { subsequence.push_back(num); } else { dividedSequences.push_back(subsequence); subsequence.clear(); subsequence.push_back(num); } } dividedSequences.push_back(subsequence); } return dividedSequences; } int main() { int length; cin >> length; vector<int> sequence(length); for (int i = 0; i < length; ++i) { cin >> sequence[i]; } vector<vector<int>> dividedSequences = divideSequence(sequence); cout << dividedSequences.size() << endl; return 0; } |