Codeforces Cinema

Codeforces Round#350(Div.2) Cinema

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

3
2 3 2
2
3 2
2 3

output

2

input

6
6 3 1 1 3 7
5
1 2 3 4 5
2 3 4 5 1

output

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 two scientists will be very pleased and all the others will be not satisfied.

题目大意

有m部电影,每部电影的语音和字幕采用不同的语言。有n个人计划去看其中一部电影,每个人只会一种语言,如果一个人能听懂电影的语音,他会很高兴;如果能看懂字幕,他会比较高兴;如果语音和字幕都听不懂,他会不开心。选择一部电影让这n个人一起看,使得很高兴的人最多,在此前提下,再让比较高兴的人最多。如果答案不唯一,输出任何一个正确答案即可。

分析

思路一:
对数组a[]排序,再借助二分查找lower_bound和upper_bound确定每种语言会的人数,通过比较选择符合条件的电影
时间复杂度O(nlog(n))

思路二:
离散化,通过分析题目发现,语言类型范围很大(1~109),而实际涉及的语言最多有2*m+n种,因此只需考虑实际存在的所有不同种类的语言。
基本步骤:
(1)将所有涉及到的语言放在一个数组中,包括电影语音字幕的语言和每个人会的语言,对语言进行排序,去重
(2)利用二分查找记录每种语言会的人数
(3)遍历所有电影,找出符合条件的电影

思路三:
借助map容器存放每种语言会的人数,避免存放大量没有涉及到的语言,造成空间上的浪费。

AC代码

思路一

#include <bits/stdc++.h>

using namespace std;

const int N=200010;
int a[N];
int b[N];
int c[N];

int n,m;

int main()
{
    
    
    ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++) cin>>b[i];
	for(int i=1;i<=m;i++) cin>>c[i];
	sort(a,a+n);
	int mx=-1;
	int ans=0;
	for(int i=1;i<=m;i++)
	{
    
    
		int y=upper_bound(a,a+n,b[i])-a;
		int x=lower_bound(a,a+n,b[i])-a;
		if(y-x>mx)
		{
    
    
			mx=y-x;
			ans=i;
		}
		if(y-x==mx&&ans>0)
		{
    
    
			int cnt1=upper_bound(a,a+n,c[i])-lower_bound(a,a+n,c[i]);
			int cnt2=upper_bound(a,a+n,c[ans])-lower_bound(a,a+n,c[ans]);
			if(cnt1>cnt2) ans=i;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

思路二

#include <bits/stdc++.h>

using namespace std;

const int N=200010;
int a[N];
int b[N];
int c[N];
int s[3*N];
int cnt[3*N];
int n,m,k,tot;

int find(int x)
{
    
    
	return lower_bound(s+1,s+k+1,x)-s;
}

int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i];
		s[++tot]=a[i];
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
    
    
		cin>>b[i];
		s[++tot]=b[i];
	}
	for(int i=1;i<=m;i++)
	{
    
    
		cin>>c[i];
		s[++tot]=c[i];
	}
	sort(s+1,s+tot+1);
	k=unique(s+1,s+tot+1)-(s+1);
	for(int i=1;i<=n;i++)
	{
    
    
		cnt[find(a[i])]++;
	}
	int ans1=1,ans2=0,ans3=0;
	for(int i=1;i<=m;i++)
	{
    
    
		int d1=cnt[find(b[i])],d2=cnt[find(c[i])];
		if(d1>ans2||(d1==ans2&&d2>ans3))
		{
    
    
			ans1=i;
			ans2=d1;
			ans3=d2;
		}
	}
	cout<<ans1<<endl;
	
	return 0;
}

思路三

#include <bits/stdc++.h>

#define mp(x,y) make_pair((x),(y))
using namespace std;

typedef pair<int,int> pii;
template<typename T, typename U> 
inline void amax(T &x, U y) 
{
    
     
	if(x < y) x = y; 
}

int main() 
{
    
    
	int n;
	while(~scanf("%d", &n)) 
	{
    
    
		map<int, int> cnt;
		vector<int> a(n);
		for(int i = 0; i < n; ++ i) {
    
    
			scanf("%d", &a[i]);
			++ cnt[a[i]];
		}
		int m;
		scanf("%d", &m);
		vector<int> b(m);
		for(int i = 0; i < m; ++ i)
			scanf("%d", &b[i]);
		vector<int> c(m);
		for(int i = 0; i < m; ++ i)
			scanf("%d", &c[i]);
		pair<pii, int> ans(mp(-1, -1), -1);
		for(int i=0;i<m;i++)
		{
    
    
			int x = cnt[b[i]];
			int y = cnt[c[i]];
			amax(ans, mp(mp(x, y), i));
		}
		printf("%d\n", ans.second + 1);
	}
	return 0;
}

注意

如果所有电影语音和字幕的语言都不在所有人会的语言中,就输出任意一部电影,即所有电影都符合要求。

猜你喜欢

转载自blog.csdn.net/weixin_46155777/article/details/113732214