CodeForces 670C 离散化入门经典题

CodeForces 670C 离散化入门经典题

【题意】:n个人,每个人会一种语言用a来表示,有m个电影,每个电影的语音是语言b,字母是语言c,其中a,b,c都是 int 级别的,如果能听懂语音,则人很高兴,如果能看懂字幕,则比较高兴,问去看哪一个电影使得在高兴的人最多的基础上,比较高兴的人最多。

【样例输入】:

3
2 3 2
2
3 2
2 3

【样例输出】:

2

【分析】:其实思路很简单,就是遍历每一个电影,假设看这部电影,求出高兴和比较高兴的人的数量,在于之前的最大值比较一下谁大即可。但是有一个问题:就是如果直接求的话因为a,b,c都是int级别的,所以直接遍历肯定是不行的。但是m,n都是2e5级别的数据,所以可以离散化数据,查询人数的时候在二分查询即可。看代码:

#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;
int n,m,a[maxn],b[maxn],c[maxn];
pii p[maxn];				//first保存语言编号,second保存会该语言的人的个数
int tot,ans;
int get_num(int x)			//二分查询
{
    int l=1,r=tot;
    while(l<r){
        int mid=(l+r)>>1;
        if(p[mid].first>=x)    r=mid;
        else l=mid+1;
    }
    return p[l].first==x?p[l].second:0;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)  scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=1;i<=m;++i)   scanf("%d",&b[i]);
    for(int i=1;i<=m;++i)   scanf("%d",&c[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;++i){      //离散化
        if(a[i]!=a[i-1]){
            p[++tot].first=a[i];
            p[tot].second=1;
        }
        else p[tot].second++;
    }
    ans=1;
    int tota=get_num(b[1]),totb=get_num(c[1]);
    for(int i=2;i<=m;++i){
        int t=get_num(b[i]),t2=get_num(c[i]);
        if(t>tota||(t==tota&&t2>totb)){
            tota=t;
            totb=t2;
            ans=i;
        }
    }
    printf("%d\n",ans);
    //system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值