Cinema CodeForces - 670C
Posted iwannabe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cinema CodeForces - 670C相关的知识,希望对你有一定的参考价值。
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
3
2 3 2
2
3 2
2 3
2
6
6 3 1 1 3 7
5
1 2 3 4 5
2 3 4 5 1
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.
题意:有n个科学家,每个人都会一种语言,用整数a【i】表示,m部电影,每个电影用b【i】语言配音,用c【i】语言填字幕,
求一个电影,他被最多的人听懂,如果有多种方案,求此情况下,被最多人看懂的(a【i】 == c【i】就可以看懂)。
思路: 我们可以想到,把科学家会的语言计数,然后对电影先按b【i】数量后按c【i】数量排序。
计数的话,我们肯定要离散化,因为 0 <= i <= 2e5
1、我们可以用map,但是注意这题是卡了unordered_map的,直接用map或者hash_map都行
1 include<bits/stdc++.h> 2 using namespace std; 3 4 int n,m; 5 const int maxn = 2e5+5; 6 int sc[maxn]; 7 8 map<int,int>mp; 9 struct Mo 10 { 11 int b; 12 int c; 13 int id; 14 } mov[maxn]; 15 16 bool cmp(Mo a,Mo b) 17 { 18 if(a.b == b.b) 19 return a.c > b.c; 20 return a.b > b.b; 21 } 22 int main() 23 { 24 scanf("%d",&n); 25 int tmp; 26 int cnt = 0; 27 for(int i=1; i<=n; i++) 28 { 29 scanf("%d",&tmp); 30 if(!mp[tmp]) 31 { 32 mp[tmp] = ++cnt; 33 sc[cnt]++; 34 } 35 else 36 sc[mp[tmp]]++; 37 } 38 scanf("%d",&m); 39 for(int i=1; i<=m; i++) 40 scanf("%d",&tmp),mov[i].id = i,mov[i].b = sc[mp[tmp]]; 41 for(int i=1; i<=m; i++) 42 scanf("%d",&tmp),mov[i].c = sc[mp[tmp]]; 43 sort(mov+1,mov+1+m,cmp); 44 printf("%d ",mov[1].id); 45 }
2、我们可以用二分离散,就是先排序a【i】,然后用unique或者手写去重,用二分查找a【i】在去重后的哪个位置,得出离散后的结果
这样我们在求取b【i】数量(或c【i】)的时候,也是二分查找b【i】(或c【i】)在去重后的位置,但是的出来位置上的电影编号不一定等于b【i】(或c【i】),
如果相等那么b【i】 = sc【pos】 (sc是之前科学家语言的计数),如果不等,b【i】 = 0(说明科学家中没人懂这语言);
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 6 int n,m; 7 const int maxn = 2e5+5; 8 int a[maxn]; 9 int b[maxn]; 10 int sc[maxn]; 11 12 struct Mo 13 { 14 int b; 15 int c; 16 int id; 17 } mov[maxn]; 18 19 bool cmp(Mo a,Mo b) 20 { 21 if(a.b == b.b) 22 return a.c > b.c; 23 return a.b > b.b; 24 } 25 int query(int x,int len) 26 { 27 return lower_bound(b+1,b+1+len,x)-b; 28 } 29 int main() 30 { 31 scanf("%d",&n); 32 for(int i=1; i<=n; i++)scanf("%d",&a[i]); 33 sort(a+1,a+1+n); 34 int tmp; 35 int len = 0; 36 for(int i=1;i<=n;i++) 37 { 38 if(i==1 || a[i] != a[i-1]) 39 b[++len] = a[i]; 40 } 41 42 for(int i=1;i<=n;i++) 43 { 44 tmp = query(a[i],len); 45 sc[tmp]++; 46 } 47 scanf("%d",&m); 48 for(int i=1; i<=m; i++) 49 { 50 scanf("%d",&tmp),mov[i].id = i; 51 int pos = query(tmp,len); 52 if(b[pos] == tmp)mov[i].b = sc[pos]; 53 else mov[i].b = 0; 54 } 55 for(int i=1; i<=m; i++) 56 { 57 scanf("%d",&tmp); 58 int pos = query(tmp,len); 59 if(b[pos] == tmp)mov[i].c = sc[pos]; 60 else mov[i].c = 0; 61 } 62 sort(mov+1,mov+1+m,cmp); 63 printf("%d ",mov[1].id); 64 }
以上是关于Cinema CodeForces - 670C的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #350 (Div. 2) C. Cinema
Codeforces 200A Cinema 并查集 + 思维 (看题解)
Codeforces 729CRoad to Cinema(二分)
CodeForces 738C Road to Cinema