• Codeforces Round #350 (Div. 2)(670C)


              今天对着算法进阶指南,学了一下离散化。大概对桶排这样的算法优化比较好吧。

    离散化:就是把无穷大的集合中若干个元素映射为有限集合以便于统计的方法。例如在很多时候,问题范围定义为整数集合Z,但涉及的元素只有m个。(桶排优化)

                此时,我们就可以把这些整数与(1-m)建立起映射关系,再去掉重复的元素。

            先排序,再删去相同的元素(也可以用unique函数)

           

    void discrete()
              {
    int m=0;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)if(i==1||a[i]!=a[i-1])
    b[++m]=a[i];
    }
    int query(int x)
    {
    return lower_bound(b+1,b+m+1,x)-b;
    }
    C. Cinema
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    input
    Copy
    3
    2 3 2
    2
    3 2
    2 3
    output
    Copy
    2
    input
    Copy
    6
    6 3 1 1 3 7
    5
    1 2 3 4 5
    2 3 4 5 1
    output
    Copy
    1
    Note

    In the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.

    In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly twoscientists will be very pleased and all the others will be not satisfied.

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    int a[200005],b[200005],c[200005],d[600005];
    vector<int>q;
    int query(int x)
    {
        return lower_bound(q.begin(),q.end(),x)-q.begin()+1;
    }
    int main() 
    {
    int n,m,ans=1,max1=0,max2=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    cin>>a[i];
    q.push_back(a[i]);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>b[i];
        q.push_back(b[i]);
    }
    for(int i=1;i<=m;i++)
    {
        cin>>c[i];
        q.push_back(c[i]);
    }
    sort(q.begin(),q.end());
    q.erase(unique(q.begin(),q.end()),q.end());//离散化 
    
    for(int i=1;i<=n;i++)
    {
        int x=query(a[i]);
        d[x]++;
    }
    for(int i=1;i<=m;i++)
    {
        int x=query(b[i]);
        int y=query(c[i]);
        if(d[x]>max1||(d[x]==max1&&d[y]>max2))
        {
            max1=d[x];
            max2=d[y];
            ans=i;
        }
    }
    cout<<ans<<endl;
       return 0;
    }

     

     

  • 相关阅读:
    电脑分辨率与pc端页面布局
    webpack打包优化并开启gzip
    JS继承实现的几种方式
    angular项目使用Swiper组件Loop时 ng-click点击事件失效处理方法
    浅谈JSONP (vue-jsonp组件 XXXtoken:报错处理)
    vue项目webpack打包后图片路径错误
    页面渲染过程详解
    vscode调试html页面,及配置说明
    跨域请求cookie获取与设置问题
    HTTP 错误 500.21
  • 原文地址:https://www.cnblogs.com/hh13579/p/10859147.html
Copyright © 2020-2023  润新知