Algorithm


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 radii 12 and 3 can be used to make a snowman but 223 or 222 cannot. Help Sergey and his twins to determine what maximum number 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.

Examples
input
Copy
7
1 2 3 4 5 6 7
output
Copy
2
3 2 1
6 5 4
input
Copy
3
2 2 3
output
Copy
0



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

int main(){

    long n; scanf("%ld", &n);
    std::map<long, long> m;
    for(long p = 0; p < n; p++){
        long r; scanf("%ld", &r);
        if(m.count(r) > 0){++m[r];}
        else{m[r] = 1;}
    }

    std::set<std::pair<long, long> > qs;
    for(std::map<long, long>::iterator it = m.begin(); it != m.end(); it++){qs.insert(std::make_pair(it->second, it->first));}
    std::vector<std::vector<long> > v;

    while(qs.size() >= 3){
        std::set<std::pair<long, long> >::iterator it = qs.end();
        it = --(qs.end()); std::pair<long, long> p1 = *it; qs.erase(it);
        it = --(qs.end()); std::pair<long, long> p2 = *it; qs.erase(it);
        it = --(qs.end()); std::pair<long, long> p3 = *it; qs.erase(it);

        --(p1.first);  if(p1.first > 0){qs.insert(p1);}
        --(p2.first);  if(p2.first > 0){qs.insert(p2);}
        --(p3.first);  if(p3.first > 0){qs.insert(p3);}

        std::vector<long> s(3); s[0] = p1.second; s[1] = p2.second; s[2] = p3.second;
        sort(s.rbegin(), s.rend()); v.push_back(s);
    }

    printf("%ld\n", v.size());
    for(long p = 0; p < v.size(); p++){printf("%ld %ld %ld\n", v[p][0], v[p][1], v[p][2]);}

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7
1 2 3 4 5 6 7

Output

x
+
cmd
2
3 2 1
6 5 4
Advertisements

Demonstration


Codeforces Solution-C. New Year Snowmen-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+