「CF670C」 Cinema 解题报告

题面

传送门

思路:

离散化hash

对于这样一个明显的统计排序的题目,当然轻而易举啦~

但是!看!语言的编号 a数组和 b数组的值最大在$10^9$的级别,所以开个数组来存———That's impossible!

所以我们可以用上离散化(也就是hash)

离散化,我们有两种写法

第一种是自己手码代码

先排序,然后去重,接着用二分一一对应,达到离散化的目的

板子:

sort(b+1,b+n+1,cmp);
n=unique(b+1,b+n+1)-b-1;
for(i=1;i<=n;i++)
    a[i]=lower_bound(b+1,b+n+1,a[i])-b;

第二种是使用STL库的map

头文件:#include <map>

定义方式:map<type,type> p;表示将前一种type映射到后一种type

其中的类型可以很多,比如double,string,int,bool等基本类型,也包括pair这种

map<pair<int,int>,bool>p;允许的

操作:一般用数组的形式,直接p[x]访问、写入

PS:但是map的速度比较慢,想进一步提高可以使用unordered_map

此处不才不做讲解,具体详见洛谷日报

Code:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int b;
int a[200010];
map<int,int> p;
int cur,res,ans;
int read()
{
    int s=0;
    char c=getchar();
    while(!isdigit(c))
        c=getchar();
    while(isdigit(c))
    {
        s=(s<<1)+(s<<3)+c-'0';
        c=getchar();
    }
    return s;
}
int main()
{
    int i;
    int x;
    n=read();
    for(i=1;i<=n;i++)
        p[read()]++;//科(珂)学家们能听懂的语言
    m=read();
    for(i=1;i<=m;i++)//电影配音
        a[i]=read();
    b=p[read()];
    ans=1;//一开始默认第一种是答案
    res=p[a[1]];
    cur=b;
    for(i=2;i<=m;i++)
    {
        b=p[read()];
        x=p[a[i]];
        if(x>=res)//比较听的懂配音,看的懂字幕的人数
        {
            if(x>res)
            {
                res=x;
                cur=b;
                ans=i;
            }
            else
                if(cur<b)
                {
                    ans=i;
                    cur=b;
                }
        }
    }
    printf("%d",ans);//跑过138个点!人憔悴~
    return 0;
}

推荐题目:

「Luogu」[NOI2015]程序自动分析

猜你喜欢

转载自www.cnblogs.com/hovny/p/10130990.html
今日推荐