天梯赛L2-039
解题思路
先看代码,再进行讲解
#include <iostream>
#include <vector>
#include <algorithm>
#include <map> // 注意:unordered_map 默认不支持 vector 作 Key,改用 map 更方便using namespace std;// 使用 vector<int> 作为键,自动处理数字大小比较
map<vector<int>, int> cnt;bool cmpl(const pair<vector<int>, int> &a, const pair<vector<int>, int> &b)
{if (a.second != b.second){return a.second > b.second; // 次数降序}return a.first < b.first; // 向量(数字序列)升序
}int main()
{ios::sync_with_stdio(false);cin.tie(0);int n, m;cin >> n >> m;for (int i = 0; i < n; i++){vector<int> row(m);for (int j = 0; j < m; j++){cin >> row[j];}cnt[row]++;}vector<pair<vector<int>, int>> res(cnt.begin(), cnt.end());sort(res.begin(), res.end(), cmpl);cout << res.size() << endl;for (int i = 0; i < res.size(); i++){cout << res[i].second;for (int j = 0; j < m; j++){cout <<" "<< res[i].first[j]; }cout << endl;}return 0;
}
