电影
https://www.acwing.com/problem/content/105/
思路:这里可以采用离散化/map的方法来记录每种语言对应的人数,然后遍历每部电影进行比较,分别用两个变量记录当前某部电影喜欢和比较喜欢的人数进行更新
离散化:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
int a[N], b[N], c[N];
vector<int> alls, add;
int find(int x){
int l = 0, r = alls.size() - 1;
while(l < r){
int mid = l + r >> 1;
if(alls[mid] >= x) r = mid;
else l = mid + 1;
}
if(alls[l] != x) return 0;
return l + 1;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
int x;
cin >> x;
alls.push_back(x);
add.push_back(x);
}
cin >> m;
for(int i = 1; i <= m; i ++) cin >> b[i];
for(int i = 1; i <= m; i ++) cin >> c[i];
// 离散化
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(), alls.end()), alls.end());
for(auto x : add) a[find(x)] ++;
int k1 = 0, k2 = 0, res = 1;
for(int i = 1; i <= m; i ++){
int x = a[find(b[i])], y = a[find(c[i])];
if(x > k1) k1 = x, k2 = y, res = i;
else if(x == k1 && y > k2) k1 = x, k2 = y, res = i;
}
cout << res << '\n';
return 0;
}
map:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
int a[N], b[N], c[N];
map<int, int> mp;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
mp[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 k1 = 0, k2 = 0, res = 1;
for(int i = 1; i <= m; i ++){
if(mp[b[i]] > k1){
k1 = mp[b[i]];
k2 = mp[c[i]];
res = i;
}
else if(mp[b[i]] == k1){
if(mp[c[i]] > k2){
k2 = mp[c[i]];
res = i;
}
}
}
cout << res << '\n';
return 0;
}