题目意思:
n个数组 最少出现m次 最多出现n次
优先取最多出现的数 如果有几个数出现次数一样 那么优先取其中最小的数
(PS:超n次我们就不用管了,就算n次 意思就是同一个数组里重复的元素不要重复计数)
想法:
开个两个map
一个map < pair<int,int>,int > m1; map <int,int> m2;
前一个map用来记录同一个数组中某个数有没有出现 (还未出现给后一个map的这个数的出现次数加个一,出现了就标记一下,不要重复加了)
只要出现次数大于m了 那么先记录下这个数 再把m改为这个数的出现次数
继续走 看看在m<=n范围 有没有出现次数更多的 如果是一样 那么看看是不是比先前记录的数小 小的话就记录下 然后继续走
走完 我们就得到了想要的答案
代码如下:
Select Code
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define fo1(a,b) for(int a=0;a<b;++a)
#define fo2(a,b) for(int a=1;a<=b;++a)
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+5;
const int mod=1e9+7;
ll a[70];
bool cmp(int a,int b){return a>b;}
int main()
{
int n,m,t,te;
while(cin>>n>>m&&(n!=0||m!=0)){
map < pair<int,int>,int > m1;
map <int,int> m2;
int ans=0,cnt=m;
fo2(i,n){
scanf("%d",&t);
fo2(j,t){
scanf("%d",&te);
if(m1[make_pair(te,i)]==0){
m2[te]++;
if(m2[te]>cnt)
ans=te,cnt=m2[te];
else if(ans==0&&m2[te]==cnt)
ans=te,cnt=m2[te];
else if(ans!=0&&m2[te]==cnt){
ans=min(ans,te);
}
}
m1[make_pair(te,i)]=1;
}
}
printf("%d\n",ans);
}
return 0;
}