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

离散化:就是把无穷大的集合中若干个元素映射为有限集合以便于统计的方法。例如在很多时候,问题范围定义为整数集合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. jvm 内存结构

    jvm 内存结构 graph TB A(jvm)-->E(类加载器系统) A-->B(运行时数据区) A-->D(本地库接口) A-->C(执行引擎) B-->虚拟机栈 ...

  2. Javascript 基础学习(四)js 的语句

    由于程序都是自上向下的顺序执行的,所以通过流程控制语句可以改变程序执行的顺序,或者反复的执行某一段的程序. 语句的分类 条件判断语句 条件分支语句 循环语句 条件判断语句 条件判断语句也称为if语句 ...

  3. dubbo初识

    1.什么是dubbo? dubbo 是一个分布式服务框架 是一个高性能的RPC框架 它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. 谈到了分布式服务框架 那 ...

  4. 使用JDBC获取数据库中的一条记录并封装为Bean

    比如我数据库中存入的是一条一条的用户信息,现在想取出一个人的个人信息,并封装为Bean对象,可以使用queryForObject来获取数据并通过new BeanPropertyRowMapper(Be ...

  5. vue自定义分页组件---切图网

    vue2.5自定义分页组件 Pagination.vue,可设置每页显示条数,带跳转框直接跳转到相应页面,亲测有用.目前很多框架自带有分页组件比如elementUI,不过在面对一个拿到PSD稿,然后重 ...

  6. Android中动态改变Listview中字体的颜色

    效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...

  7. windows本地安装Oracle数据库

    一.下载Oracle 11g R2 for Windows. 官方网站: https://www.oracle.com/database/technologies/oracle-database-so ...

  8. R语言常用的矩阵操作

    R语言是一门非常方便的数据分析语言,它内置了许多处理矩阵的方法.下面列出一些常用的矩阵操作方法示例. 矩阵的生成 > mat <- matrix(:, ncol = , nrow = , ...

  9. LINUX系统(CentOS7安装)一之JDK8的安装

    JDK 的安装系统安装就不做过多介绍,大家从网上寻找安装步骤进行安装,不过我推荐大家进行安装时选择桌面图形化格式进行安装,方便做一部分操作,同时在安装过程中选择英文模式,同时我自己在安装的过程中发现使 ...

  10. 关于非旋转Treap

    刚刚跟着EM-LGH大佬学了非旋转Treap 非常庆幸不用再写万恶的rotate了(来自高级数据结构的恶意) 来记一下 Treap 概念 简单来说,\(Tree_{二叉搜索树} * Heap_堆 = ...