cf 100 c(greedy)

C. New Year Snowmen
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii12 and 3 can be used to make a snowman but 223 or 222 cannot. Help Sergey and his twins to determine what maximumnumber of snowmen they can make from those snowballs.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1r2, ...,rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

Output

Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.

Sample test(s)
input
7
1 2 3 4 5 6 7
output
2
3 2 1
6 5 4
input
3
2 2 3
output
0

选择当前出现次数最多的三个数,用一个优先队列维护即可。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include <iostream>
#include<algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;

struct Node{
    int id,sum;
    bool operator < (const Node & a) const{
        return sum < a.sum;
    }
};

map<int,int> M;
map<int,int>::iterator it;
vector<int> ans[maxn];
priority_queue<Node> Q;

int main(){
    int n;
    while(cin >> n){
        M.clear();
        while(!Q.empty()) Q.pop();
        for(int i = 0;i < n;i++) ans[i].clear();
        for(int i = 0;i < n;i++){
            int tem;
            cin >> tem;
            M[tem]++;
        }
        for(it = M.begin();it != M.end();it++){
            Q.push(Node{it->first,it->second});
        }
        int cnt = 0;
        while(Q.size()>=3){
            Node A[3];
            A[0] = Q.top();Q.pop();
            A[1] = Q.top();Q.pop();
            A[2] = Q.top();Q.pop();
            for(int i = 0;i < 3;i++) ans[cnt].push_back(A[i].id);
            sort(ans[cnt].begin(),ans[cnt].end(),greater<int>());
            cnt++;
            for(int i = 0;i < 3;i++){
                A[i].sum--;
                if(A[i].sum > 0) Q.push(A[i]);
            }
        }
        cout << cnt << endl;
        for(int i = 0;i < cnt;i++){
            for(int j = 0;j < 3;j++) cout << ans[i][j] << ' ';
            cout << endl;
        }
    }
    return 0;
}


你可能感兴趣的:(cf 100 c(greedy))