CodeForces 140 C. New Year Snowmen 详解 (贪心+优先队列)




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 whatmaximum 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
7
1 2 3 4 5 6 7
output
2
3 2 1
6 5 4
input
3
2 2 3
output
0


题目大意】给你一个整数序列,求最多选出每个长度为3的且序列元素单调的子序列的个数,并且输出每个子序列的元素,作为一个子序列,每个元素只能选一次,也就是满足一次性,但每个子序列里可以存在相同的元素。

思路: 贪心策略肯定是,取数量最多的3个,比如:10 10 1 1 1 1  1 1,如果取小的话,那么6个1两次就取完了,如果取最大的话,6个1可以取6次,这就是为什么要取数量最多的半径。。因为这个策略是一定的,每次取完之后,数量会改变,数量最大的也不是原来的了,这时候就要用优先队列了,每次拿完之后自动排序(再次膜潘学长)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1e5;
int a1[maxn], b1[maxn], c1[maxn];
struct node
{
    int num, r;
    node(){}
    node(int nn, int rr) :num(nn), r(rr){}
    bool operator < (const node &a) const
    {
        if(num != a.num)  //按照数量排序
            return num < a.num;
        else
            return r < a.r;  //数量一样最大的在前面
    }
};
int main()
{

    int n;
    while(~scanf("%d", &n))
    {
        map m;
        int a;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a);
            m[a]++;  //这里一开始想用book,发现半径1e9,会爆数组 就用map了
        }
        priority_queue q;
        for(map::iterator it = m.begin(); it != m.end(); it++)  //迭代器,把下表跟值都压进去
        {
            q.push(node(it->second, it->first));
        }
        if(q.size() < 3) //一开始小于3肯定不行
        {
            printf("0\n");
            continue;
        }
        int sum = 0, index = 0;
        while(q.size() >= 3)
        {
            node a = q.top();  //取前三个
            q.pop();
            node b = q.top();
            q.pop();
            node c = q.top();
            q.pop();
            sum++;
            a1[index] = a.r;
            b1[index] = b.r;
            c1[index++] = c.r;
            if(a.num-1 != 0) q.push(node(a.num-1, a.r));  //如果前三个-1之后不为0就继续压进队列
            if(b.num-1 != 0) q.push(node(b.num-1, b.r));
            if(c.num-1 != 0) q.push(node(c.num-1, c.r));
        }
        printf("%d\n", sum);
        for(int i = 0; i < index; i++)
        {
            if(a1[i] < b1[i]) swap(a1[i], b1[i]);  //别忘记这一步。。。
            if(a1[i] < c1[i]) swap(a1[i], c1[i]);
            if(b1[i] < c1[i]) swap(b1[i], c1[i]);
        }
        for(int i = 0; i < index; i++)
        {
            printf("%d %d %d\n", a1[i], b1[i], c1[i]);
        }
    }
    return 0;
}


你可能感兴趣的:(CF,stl,ACM)