F - 我们什么时候能见面?

题目意思:
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值