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

离散化:就是把无穷大的集合中若干个元素映射为有限集合以便于统计的方法。例如在很多时候,问题范围定义为整数集合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. drf token刷新配置、认证组件(使用)、权限组件(使用)、频率组件(使用)、异常组件(使用)

    目录 一.特殊路由映射的请求 二.token刷新机制配置(了解) 三.认证组件项目使用:多方式登录 1.urls.py 路由 2.views.py 视图 3.serializers.py 序列化 4. ...

  2. 「C++ 」借来的资源,何如还的潇洒?

    前言 本文的内容将专门对付内存管理,培养起有借有还的好习惯,方可消除资源管理的问题. 正文 所谓的资源就是,一旦用了它,将来必须还给系统.如果不是这样,糟糕的事情就会发生. C++ 程序内常见的资源: ...

  3. Java面试—消息队列

    消息队列面试题 题目来自于中华石杉,解决方案根据自己的思路来总结而得. 题目主要如下: 1. 为什么要引入消息队列? 消息队列的引入可以解决3个核心问题: 解耦 异步 削峰 解耦 在一个项目中,如果一 ...

  4. 【Java并发工具类】原子类

    前言 为保证计数器中count=+1的原子性,我们在前面使用的都是synchronized互斥锁方案,加锁独占访问的方式未免太过霸道,于是我们来介绍另一种解决原子性问题的无锁方案:原子变量.在正式介绍 ...

  5. opencv —— calcHist、minMaxLoc 计算并绘制图像直方图、寻找图像全局最大最小值

    直方图概述 简单来说,直方图就是对数据进行统计的一种方法,这些数据可以是梯度.方向.色彩或任何其他特征.它的表现形式是一种二维统计表,横纵坐标分别是统计样本和该样本对应的某个属性的度量. 计算直方图: ...

  6. [WPF 自定义控件]创建包含CheckBox的ListBoxItem

    1. 前言 Xceed wpftoolkit提供了一个CheckListBox,效果如下: 不过它用起来不怎么样,与其这样还不如参考UWP的ListView实现,而且动画效果也很好看: 它的样式如下: ...

  7. Redis 为什么这么快?

    1. 纯内存操作,肯定快 数据存储在内存中,读取的时候不需要进行磁盘的 IO 2. 单线程,无锁竞争损耗 单线程保证了系统没有线程的上下文切换 使用单线程,可以避免不必要的上下文切换和竞争条件,没有多 ...

  8. ES6 - 基础学习(6): 对象扩展

    对象对于JavaScript至关重要,在ES6中对象又加了很多新特性. 对象字面量:属性的简洁表示法 ES6允许对象的属性直接写变量,这时候属性名是变量名,属性值是变量值. let attr1 = & ...

  9. 5.Android-电话拨号器详解

    之前学习了3.Android-ADT之helloworld项目结构介绍后,本章便来写个简单的电话拨号器程序. 实现的步骤如下所示: 1.创建项目 2.写layout/activity_main.xml ...

  10. 【python基础语法】OS模块处理文件绝对路径,内置的异常类型、捕获、处理(第9天课堂笔记)

    import os """ 通过文件的路径去打开文件 相对路径:相对当前的工作路径去定位文件位置 .:代表当前路径 ..:代表上一级路径(父级路径) 绝对路径:相对于电脑 ...