"Huawei Cup" China University of Mining and Technology Program Design Contest G graduates' commemorative gift

Topic

Insert picture description here

answer

  1. The question requires that we only need to send 3 different types of items to the same student, then we only need to count the number of each item.

  2. Because we want to send the most people, we have to send the more types first, and the less types later, so that we can ensure that the 3 pairs that can be assembled are the most, and we can directly use the priority queue.

Code

#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;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/113887437