top1编程
← 返回题目
题解

电影

1 条题解

  • 0
    @ 2026-7-28 22:44:56

    P2249 电影

    解题思路

    先统计每种语言对应的科学家人数。对每部电影,分别查询能听懂配音和能看懂字幕的人数,按这两个数从大到小比较,保留最优电影编号。配音和字幕语言不同,因此两类人数不重叠。

    参考代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        ios::sync_with_stdio(false); cin.tie(nullptr);
        int n; cin >> n;
        unordered_map<int,int> count;
        count.reserve(n * 2 + 1);
        for (int i=0, x; i<n; ++i) { cin >> x; ++count[x]; }
        int m; cin >> m;
        vector<int> audio(m), subtitle(m);
        for (int &x:audio) cin >> x;
        for (int &x:subtitle) cin >> x;
        int best=0, bestAudio=-1, bestSubtitle=-1;
        for (int i=0; i<m; ++i) {
            int a=count[audio[i]], b=count[subtitle[i]];
            if (a>bestAudio || (a==bestAudio && b>bestSubtitle)) {
                best=i; bestAudio=a; bestSubtitle=b;
            }
        }
        cout << best+1 << '\n';
    }
    

    复杂度分析

    使用哈希表,平均时间复杂度O(n+m),空间复杂度O(n+m)。

    • 1