题面:
https://ac.nowcoder.com/acm/contest/1001/A
大意:
n个人去电影院,每个人有自己喜欢的语言(用数字表示)——a[n]。
m个电影院,电影院有audio——b[m]和subtitles——c[m], 人与audio相同高兴,人与subtitles相同比较高兴。
求使最多人高兴的电影(b在a的体现),当b相等时求比较高兴的人多的电影(b相等情况下,c在a中的体现)。
思路:
因为a,b,c的数据范围都是[1,1e9],所以采取离散化或者用map存储
离散化后语言种数最多n + 2m
map直接存储后进行模拟
代码:
离散化:
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 2e5 + 9;
int n, m;
int a[MAXN], b[MAXN], c[MAXN];
int unite[3 * MAXN], unite_a[3 * MAXN];
int find(int len, int x) //在离散化数组中寻找值
{
return lower_bound(unite + 1, unite + len + 1, x) - unite;
}
int discrete() //对数组进行离散化处理
{
int len = 0;
for (int i = 1; i <= n; i++) //存a数组
unite[++len] = a[i];
for (int i = 1; i <= m; i++) //存b,c数组
{
unite[++len] = b[i];
unite[++len] = c[i];
}
sort(unite + 1, unite + len + 1); //去重前先排序
return unique(unite + 1, unite + len + 1) - (unite + 1); //离散化数组去重,返回离散化数组长度
}
int main()
{
cin >> n;
for (int i = 1; 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];
int unite_len = discrete(); //求得离散化数组的长度
for (int i = 1; i <= n; i++)
unite_a[find(unite_len, a[i])]++; //a中每个值在离散化数组的个数
int max_b = 0, max_c = 0;
int index;
for (int i = 1; i <= m; i++)
{
int find_b = unite_a[find(unite_len, b[i])]; //b在a中的体现
int find_c = unite_a[find(unite_len, c[i])]; //c在a中的体现
if (find_b > max_b || find_b == max_b && find_c > max_c)
{
index = i;
max_b = find_b;
max_c = find_c;
}
}
cout << index << endl;
return 0;
}
map:
#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
const int MAXN = 2e5 + 9;
int n, m;
int a[MAXN], b[MAXN], c[MAXN];
map<int,int>mp;
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
mp[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 max_b=0, max_c=0, index;
for(int i=1; i<=m; i++){
if(mp[b[i]]>max_b || mp[b[i]]==max_b&&mp[c[i]]>max_c){
index=i;
max_b=mp[b[i]];
max_c=mp[c[i]];
}
}
printf("%d\n", index);
return 0;
}