CodeForces 140C New Year Snowmen

41 篇文章 0 订阅
21 篇文章 0 订阅

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.

Example
Input
7
1 2 3 4 5 6 7
Output
2
3 2 1
6 5 4
Input
3
2 2 3
Output

0


同今年chinafinal上的某题,二分答案贪心验证

#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1e5+10;
int n,a[N],g[N][3];

bool check(int x)
{
    if(x==0) return 1;
    for (int i=1;i<=x;i++) g[i][0]=a[i];
    int k=1;
    for (int i=x+1,j=1;i<=n;i++)
    {
        if (g[j][k-1]==a[i]) continue;
        g[j++][k]=a[i];
        if (j>x) j-=x,k++;
        if (k>2) break;
    }
    return k>2;
}

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int l=0,r=n/3;
    while (l<=r)
    {
        int mid=l+r>>1;
        if (check(mid)) l=mid+1; else r=mid-1;
    }
    check(r);
    printf("%d\n",r);
    for (int i=1;i<=r;i++)
        for (int j=2;j>=0;j--) printf("%d%s",g[i][j],j?" ":"\n");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值