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

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

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

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

void discrete()
{
int m=0;
sort(a+,a+n+);
for(int i=;i<=n;i++)if(i==||a[i]!=a[i-])
b[++m]=a[i];
}
int query(int x)
{
return lower_bound(b+,b+m+,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[],b[],c[],d[];
vector<int>q;
int query(int x)
{
return lower_bound(q.begin(),q.end(),x)-q.begin()+;
}
int main()
{
int n,m,ans=,max1=,max2=;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
q.push_back(a[i]);
}
cin>>m;
for(int i=;i<=m;i++)
{
cin>>b[i];
q.push_back(b[i]);
}
for(int i=;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=;i<=n;i++)
{
int x=query(a[i]);
d[x]++;
}
for(int i=;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 ;
}

Codeforces Round #350 (Div. 2)(670C)的更多相关文章

  1. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

  2. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

  3. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  4. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)

    题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...

  5. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  6. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  7. Codeforces Round #350 (Div. 2) C. Cinema 水题

    C. Cinema 题目连接: http://www.codeforces.com/contest/670/problem/C Description Moscow is hosting a majo ...

  8. Codeforces Round #350 (Div. 2) B. Game of Robots 水题

    B. Game of Robots 题目连接: http://www.codeforces.com/contest/670/problem/B Description In late autumn e ...

  9. Codeforces Round #350 (Div. 2) A. Holidays 水题

    A. Holidays 题目连接: http://www.codeforces.com/contest/670/problem/A Description On the planet Mars a y ...

随机推荐

  1. NCE L6

  2. 01.JS语法规范、变量与常量

    前言:    学习一门编程语言的基本步骤   (01)了解背景知识  (02)搭建开发环境  (03)语法规范  (04)常量和变量 2.JS的开发环境 (1)浏览器自带的JS解释器(js引擎) (2 ...

  3. docker之阿里云centos 7.x 启动容器报错处理办法

    最近阿里云服务器(操作系统centOS 7.x) 安装docker,参照阿里云帮助文档https://help.aliyun.com/document_detail/51853.html?spm=a2 ...

  4. centos tomcat解压版安装

    解压: tar -xzvf apache-tomcat-8.5.23.tar.gz -C /usr/local/java 配置Tomcat的环境变量: export CATALINA_HOME=/us ...

  5. fatal: HttpRequestException encountered

    报错:fatal: HttpRequestException encountered 解决方法 Github 禁用了TLS v1.0 and v1.1,必须更新Windows的git凭证管理器,才行. ...

  6. 清北学堂—2020.1提高储备营—Day 4 morning(数论)

    qbxt Day 4 morning --2020.1.20 济南 主讲:李奥 目录一览 1.一些符号与基本知识 2.拓展欧几里得,逆元与欧拉定理 3.线性筛法与积性函数(非重点) 总知识点:数论 一 ...

  7. 使用JAVA导出EXCEL表格(POI)

    一.POI概述 Jakarta POI 是一套用于访问微软格式文档的Java API.POI提供API给Java程序对Microsoft Office格式档案读和写的功能.在许多企业办公系统中,经常会 ...

  8. Kali linux中安装字体的一种方法

    有人说,lucida sans typewriter字体是一种非常适合的编程字体,所以想试试.经过一番折腾,终于在Clion中用上了.步骤如下: 第一步:先下载这个字体. http://www.dow ...

  9. QQ常用表情

    以下表情均为QQ官方表情原图,版权归QQ所有,禁止用于商业用途. ![3nEdY9.png](https://s2.ax1x.com/2020/02/21/3nEdY9.png) ![3nEaFJ.p ...

  10. MVC的App_Data中看不到数据库mdf文件

    点击运行后的页面去注册个账号,然后点击解决方案的‘显示所有文件就能看到了