每日一题——Cinema

题目

Cinema

题解

用一个cnt数组来记录每个出现过的语言有多少科学家懂得,然后遍历一遍电影,设变量maxn为有多少人很开心,如果现在遍历的b[i]种语言比之前的某种语言有更多的科学家懂,那就更新maxn=cnt[b[i]],res记录的是第几部电影;如果现在遍历的b[i]种语言和之前的某种语言懂的科学家一样多,那就看哪部能让更多人比较开心,若当前这部满足就更新maxn和res。最后输出res就可以了。

参考代码(map版)

#include<iostream>
#include<map>
using namespace std;
const int N = 2e5 + 10;
int n, m;
int a[N], b[N], c[N];
map<int,int> cnt;
int main(){
    cin >> n;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        cnt[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 maxn = 0, res = 0;
    for(int i = 1; i <= m; i ++){
        if(maxn < cnt[b[i]]){
            maxn = cnt[b[i]];
            res = i;
        }
        if(maxn == cnt[b[i]] && cnt[c[res]] < cnt[c[i]]){
            maxn = cnt[b[i]];
            res = i;
        }
    }
    cout << res << endl;
    return 0;
}

对于离散化我们有个模板

vector<int> alls; // 存储所有待离散化的值
sort(alls.begin(), alls.end()); // 将所有值排序
alls.erase(unique(alls.begin(), alls.end()), alls.end());   // 去掉重复元素

// 二分求出x对应的离散化的值
int find(int x) // 找到第一个大于等于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;
    }
    return r + 1; // 映射到1, 2, ...n
}

参考代码(离散化)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5 + 10;
int n, m;
int cnt[N * 3], 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;
    }
    return r;
}
int main(){
    cin >> n;
    while(n --){
        int t;
        cin >> t;
        alls.push_back(t);
        add.push_back(t);
    }
    cin >> m;
    for(int i = 0; i < m; i ++){
        cin >> b[i];
        alls.push_back(b[i]);
    }
    for(int i = 0; i < m; i ++){
        cin >> c[i];
        alls.push_back(c[i]);
    }
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    for(auto i : add) cnt[find(i)] ++;
    int res = 0, Like = 0, like = 0;
    for(int i = 0; i < m; i ++){
        int L = cnt[find(b[i])], l = cnt[find(c[i])];
        if(Like < L) Like = L, like = l, res = i;
        else if(Like == L && like < l) like = l, res = i;
    }
    cout << res + 1<< endl;
    return 0;
}
posted @ 2025-03-13 18:33  PZnwbh  阅读(11)  评论(0)    收藏  举报