【离散化】 离散化及CodeForces 670C Cinema(/map)详解

题目链接:https://vjudge.net/problem/CodeForces-670C

Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.

In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different).

Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows.

The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.

The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie.

The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie.

It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Output

Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.

If there are several possible answers print any of them.

题目大意:

n个科学家每个人都掌握一种语言,要从m部电影中选一部一起看,每部电影的语音和字幕采用了不同的语言,现在要一部电影,让尽可能多的科学家能听懂电影的语音,如果满足这个条件的电影有多部,选其中能让尽可能多的科学家看懂字幕的电影

分析:

第一反应是桶排序,记录会某种语言的科学家有多少个。

但是这道题表示语言的数字范围在10的9次方,直接桶排序肯定会内存超限。

注意到虽然语言的范围很大,但是语言的种类是很少的,只有2*10的5次方。(直接用桶排序的话大部分空间都被浪费了)

所以可以使用离散化:

将无穷大集合中的若干数映射为有限集合以便于统计

换言之,比如有:2,78372,12391028,23773,1830919248,23773五个数,统计其中出现次数最多的数

我们还是用桶排序的思想,让一个buc数组存放每个数的数量

但是我们给2编号1,给78372编号2,给12391028编号3,给1830919248编号4

让buc[1]存放2的数量,让buc[2]存放78372的数量……

就避免了内存超限的问题

离散化具体实现:

①将所有可能出现的数放到数组d中

②排序并去重

③二分查找下标,即实现映射

即将一个很大的数映射为它在数组ret(即去重后的d数组)中的位置

假设有一个很大的数字10892813918,则当我们要应用桶排序统计10892813918出现次数的时候,就只要让buc[Query(10892813918)]++就行了。

int cnt=0;

void discrete(){
    sort(d,d+t);
    for(int i=0;i<t;i++)
         if(!i||d[i]!=d[i-1])
                     ret[cnt++]=d[i];
}

int Query(int x){
   return lower_bound(ret,ret+cnt,x)-ret;
}

离散化AC代码:

这道题目有一个坑点:

就是有可能出现任意一部电影,都没有科学家能看懂或听懂的情况,但你还是必须选出一部电影

#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;

const int maxn=2e5+10;
int n,m;

int a[maxn];
int b[maxn];
int c[maxn];
int d[3*maxn];
int ret[3*maxn];
int buc[maxn];
int cnt=0;
int t=0;

void discrete(){
    sort(d,d+t);
    for(int i=0;i<t;i++)
         if(!i||d[i]!=d[i-1])
                     ret[cnt++]=d[i];
}

int Query(int x){
   return lower_bound(ret,ret+cnt,x)-ret;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
       scanf("%d",&a[i]),d[t++]=a[i];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
       scanf("%d",&b[i]),d[t++]=b[i];
    for(int i=1;i<=m;i++)
       scanf("%d",&c[i]),d[t++]=c[i];

    discrete();
    memset(buc,0,sizeof(buc));
    for(int i=0;i<n;i++)
         buc[Query(a[i])]++;

    int max_audio=-1;
    int max_subtitile=-1;
    int ans=0;
    for(int i=1;i<=m;i++){
        if(buc[Query(b[i])]>max_audio)
              max_audio=buc[Query(b[i])],max_subtitile=buc[Query(c[i])],ans=i;
        else if(buc[Query(b[i])]<max_audio)  continue;
        else if(buc[Query(c[i])]>max_subtitile)    max_subtitile=buc[Query(c[i])],ans=i;
    }
    printf("%d\n",ans);
    return 0;
}

 另外用map也是可以在不内存超限的情况下实现这样的映射的,但耗时多了点,过还是能过的

 map做法代码如下:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

const int maxn=2e5+10;

int a[maxn];
int b[maxn];
int c[maxn];

map<int,int> buc;

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
       scanf("%d",&a[i]),buc[a[i]]++;
    int m;
    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]);
    int max_audio=-1;
    int max_subtitile=-1;
    int ans=0;
    for(int i=1;i<=m;i++){
        if(buc[b[i]]>max_audio)
              max_audio=buc[b[i]],max_subtitile=buc[c[i]],ans=i;
        else if(buc[b[i]]<max_audio)  continue;
        else if(buc[c[i]]>max_subtitile)    max_subtitile=buc[c[i]],ans=i;
    }
    printf("%d\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值