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.
题目大意:有n个人去看电影,一共有m部电影,每部电影的语音和字幕采用不相同的语言,这N个人说不同的语言,如果一个人能听懂语言会很高兴,看懂字幕会比较高兴。选择一部电影,让很高兴的人尽量多,如果答案不唯一在让比较高兴的人最多。
大致思路:这道题有两种解法,第一种:我们可以用一个map数组记录所有电影和人涉及的语言的出现的次数,然后统计出最多的。第二种解法:我们可以把所有电影和人涉及的语言放进一个数组,然后排序离散化,用数组直接统计每种语言的人的数量。
第一种解法运行结果及代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int a[MAXN],b[MAXN],c[MAXN];
map<int,int> cnt;
int n,m;
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
scanf("%d",&m);
for(int i = 1; i <= m; i++)
scanf("%d",&b[i]);
for(int i = 1; i <= m; i++)
scanf("%d",&c[i]);
int ans = 1, maxx1 = cnt[b[1]], maxx2 = cnt[c[1]];
for(int i = 2; i <= m; i++){
int x = cnt[b[i]];
int y = cnt[c[i]];
if(x >= maxx1){
if(x > maxx1){ ans = i; maxx1 = x; maxx2 = y; continue; }
else {
if(y > maxx2){ ans = i; maxx1 = x; maxx2 = y; continue; }
}
}
}
printf("%d\n",ans);
return 0;
}
第二种解法:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int a[MAXN],b[MAXN],c[MAXN],d[MAXN * 3],ans[MAXN * 3];
int cnt[MAXN];
int n,m,counts;
void discrete(){
counts = 0;
sort(d + 1, d + 2 * m + n + 1);
for(int i = 1; i <= 2 * m + n; i++){
if(i == 1 || d[i] != d[i - 1])
ans[++counts] = d[i];
}
}
int Query(int x){ return lower_bound(ans + 1,ans + counts + 1, x) - ans; }
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
d[i] = a[i];
}
scanf("%d",&m);
for(int i = 1; i <= m; i++){
scanf("%d",&b[i]);
d[n + i] = b[i];
}
for(int i = 1; i <= m; i++){
scanf("%d",&c[i]);
d[m + n + i] = c[i];
}
//for(int i = 1; i <= 2 *m + n ;i++)
//cout << d[i] << endl;
discrete();
for(int i = 1 ; i <= n; i++){
cnt[Query(a[i])]++;
//cout << Query(a[i]) << endl;
}
int ans = 1,maxx1 = cnt[Query(b[1])],maxx2 = cnt[Query(c[1])];
for(int i = 2; i <= m; i++){
int x = cnt[Query(b[i])];
int y = cnt[Query(c[i])];
if(x >= maxx1){
if(x > maxx1) { ans = i ; maxx1 = x; maxx2 = y;continue; }
else {
if(y > maxx2){ ans = i; maxx1 = x; maxx2 = y; continue; }
}
}
}
printf("%d\n",ans);
return 0;
}