题目描述
微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性。本题就要求你写个程序,通过统计一个人点赞的纪录,分析这个人的特性。
输入格式:
输入在第一行给出一个正整数N(≤1000),是该用户点赞的博文数量。随后N行,每行给出一篇被其点赞的博文的特性描述,格式为“K F1⋯FK”,其中1≤K≤10,Fi(i=1,⋯,K)是特性标签的编号,我们将所有特性标签从1到1000编号。数字间以空格分隔。
输出格式:
统计所有被点赞的博文中最常出现的那个特性标签,在一行中输出它的编号和出现次数,数字间隔1个空格。如果有并列,则输出编号最大的那个。
代码
#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;
int n, k, f;
int cnt[N], res;
int main(){
cin >> n;
while(n--){
cin >> k;
while(k--){
cin >> f;
++cnt[f];
if(cnt[f] > cnt[res]) //最常出现的标签
res = f;
else if(cnt[f] == cnt[res] && f > res) //编号更大的那个
res = f;
}
}
cout << res << " " << cnt[res];
return 0;
}
代码(使用优先队列)
#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;
int n, k, f;
int cnt[N];
priority_queue<PII> q; //默认从大到小
int main(){
cin >> n;
while(n--){
cin >> k;
while(k--){
cin >> f;
q.push({++cnt[f], f});
}
}
cout << q.top().second << " " << q.top().first;
return 0;
}