a583簡單排序正規化與最高頻率總人數
CPE:Conformity (UVa 11286)
把不同排列的同一課程集合映成同一鍵。
解題思路
每位學生的五門課先由小到大排序,將排序後的五元組當作組合鍵。因為課程皆不同,兩人選同一組課若且唯若排序結果相同,所以可以用計數映射累計人氣。
讀完先找最高頻率best,再將所有頻率等於best的組合人數相加。若三種組合各一人,best=1但答案是3,不是1;若只有一種最高頻率2,答案才是2。n=0結束,每組只印總人數一行,不印課程或最高頻率。
Step-by-step animation
示意:四人分別選A、A、B、B兩種五門課組合
步驟 1 / 3
前兩人
兩人的課程排序後相同,A人氣2。
A:2
- 學生數
- 2
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;
int main() {
int n;
while (cin >> n && n) {
map<array<int, 5>, int> freq;
while (n--) {
array<int, 5> a;
for (int &x : a)
cin >> x;
sort(a.begin(), a.end());
++freq[a];
}
int best = 0, ans = 0;
for (auto &entry : freq)
best = max(best, entry.second);
for (auto &entry : freq)
if (entry.second == best)
ans += entry.second;
cout << ans << '\n';
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。