CodeForces - 670C

本文介绍了一种使用离散化和二分查找的方法来解决一个关于电影选择的问题。问题要求选出能让最多科学家理解其语言的电影。通过离散化处理大量不连续的语言种类,并利用二分查找快速定位每种语言对应的科学家数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://vjudge.net/problem/CodeForces-670C
对于我这个新手,此题略难。
(话说这个不是离散化+二分吗,怎么被划入排序了)

题目概括

一群科学家挑选电影看。每个科学家只会一种语言(用序号表示)。一部电影有两种语言:音频语言与文字语言。问选取那部电影可以使得看懂音频语言的科学家最多。(若同样多,则选取使得看懂文字语言的科学家最多的电影)

思路

首先,我们必须知道每一种语言掌握的人数是多少。显然,桶排序是一个不错的选择。
可惜,给的区间范围是[1,1e9]。所以要离散化。
怎么离散化?
我们把所有科学家掌握的语言的序号以及电影中所用到的语言的序号都存起来(注意:电影中所用到的语言的序号也要存起来,这样在接下来的二分查找中能够直接用lower_bound函数去二分,不然很麻烦。。。)。排序,去重,得到一个新的数组。这就完成了离散化。

在这里插入图片描述
上面这张就是离散化过程。。。1->1,3->2,6->3,7->4
剩下就是依葫芦画瓢好了。

#include <bits/stdc++.h>

using namespace std;
const int maxn=2e5+5;

int n,m,cnt,a[maxn],b[maxn],c[maxn],temp[3*maxn],ncnt;
int num[3*maxn],sum[3*maxn];///num记录按顺序排列的不同的数字
///sum记录对应每一个num有多少个数

int Find_pos(int u)
{
    return lower_bound(num+1,num+1+ncnt,u)-num;
}
void solve()
{
    sort(temp+1,temp+cnt+1);
    int i;
    for(i=1;i<=cnt;i++)
    {
        if(i==1||temp[i-1]!=temp[i])
        {
            num[++ncnt]=temp[i];
        }
    }
    for(i=1;i<=n;i++)
    {
        int u=Find_pos(a[i]);///通过二分查找很快找到确定a[i]在数组num中的位置
        sum[u]++;
    }
    int max_one=-1,max_two=-1,ans;
    for(i=1;i<=m;i++)
    {
        int fir=Find_pos(b[i]),sec=Find_pos(c[i]);
        if(sum[fir]>max_one)
        {
            max_one=sum[fir];
            max_two=sum[sec];
            ans=i;
        }
        else if(sum[fir]==max_one&&sum[sec]>max_two)
        {
            max_two=sum[sec];
            ans=i;
        }
    }
    printf("%d\n",ans);
}
int main()
{
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        temp[++cnt]=a[i];
    }
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
        temp[++cnt]=b[i];///注意b,c也装进temp数组
    }
    for(i=1;i<=m;i++)
    {
        scanf("%d",&c[i]);
        temp[++cnt]=c[i];
    }
    solve();
    return 0;
}

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值