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

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

    问题描述: Pete likes to bake some cakes. He has some recipes and ingredients. Unfortunately he is not go ...

  2. PPT导出为图片

    使用Aspose组件导出 Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件. Aspose可能通过Aspose.Slides.NET安装 简单的导出图片d ...

  3. AndroidStudio中使用XML和Java代码混合控制UI界面实现QQ相册照片列表页面

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建Androi ...

  4. Jmeter后置处理器,正则表达式提取器的使用

    [使用场景]:下一个请求参数需要从上一个请求的响应数据中获取 [jmeter正则表达式说明]:使用perl正则表达式(可参考:http://www.runoob.com/perl/perl-regul ...

  5. Kong 系列【六】添加插件---ip-restriction之黑白名单

    写在前边 本地postMan请求http://192.168.130.131:8000/test-route,可以正常访问,本地IP:192.168.130.1同样在虚拟机环境192.168.130. ...

  6. 为什么你SQL Server中SQL日期转换出错了呢?

    开发人员有时候使用类似下面SQL将字符串转换为日期时间类型,乍一看,这样的SQL的写法是没有什么问题的.但是这样的SQL其实有时候就是一个定时炸弹,随时可能出现问题(),下面简单对这种情况进行一个简单 ...

  7. 硬件知识整理part2--电阻在反馈网络中的应用

    学而不厌,诲人不倦,不知老之将至.--孔子 电阻作为电路中基本的元器件之一.在电路设计中,我们有时会使用欧姆定律来大致估计一下电阻值的大小,但是大多时候我们是不用去过多考虑电阻值的大小,像我这样大方的 ...

  8. PMP--1.2 PMBOK指南组成部分

    图1.2.5 ​ PMBOK指南关键组成部分在项目中的相互关系说明:项目生命周期中包含项目阶段,项目阶段结束之后就是阶段关口: 而项目管理过程和项目管理过程组以及项目管理知识领域都是为了项目生命周期服 ...

  9. 基于java开发jsp+ssm+mysql实现的在线考试系统 源码下载

    实现的关于在线考试的功能有:用户前台:用户注册登录.查看考试信息.进行考试.查看考试成绩.查看历史考试记录.回顾已考试卷.修改密码.修改个人信息等,后台管理功能(脚手架功能不在这里列出),科目专业管理 ...

  10. Linux systemctl系统工具常用总结(详)

    systemctl是一个系统自带的服务管理工具,可以管理系统的服务的,启动.停止.重启.自启.监视.也可以对脚本程序后台运行管理. 文章以nginx.service举例 基础命令: systemctl ...