“华为杯”中国矿业大学程序设计学科竞赛 G 毕业生的纪念礼物

题面

在这里插入图片描述

题解

  1. 题中要求我们只需要将3中不同种类的物品发给同一个学生,那么我们只要统计每种物品的个数即可,

  2. 因为我们要使发的人最多,所以每次就要使种类多的先发,种类少的后发,这样就可以保证能凑齐的3个一对最多,直接用优先队列即可

代码

#include<bits/stdc++.h>

using namespace std;
map<int, int> m;
priority_queue<int> q;

int main() {
    
    
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
    
    
        int x;
        cin >> x;
        m[x]++;
    }
    for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    
    
        q.push(it->second);
    }


    int ans = 0;
    while (q.size() > 2) {
    
    
        int x = q.top();
        q.pop();
        int y = q.top();
        q.pop();
        int z = q.top();
        q.pop();
        x--, y--, z--;
        if (x) q.push(x);
        if (y) q.push(y);
        if (z) q.push(z);
        ans++;
    }
    cout << ans << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/113887437
今日推荐