Codeforces 140C 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


题目大意

 给你一个整数序列,问其中严格递增的长度为三的子序列有多少个并输出。


做法分析

 分析可知,序列个数一定为,其中max1为序列中出现次数最多的值,max2为序列中出现次数次多的值。由此可以推出答案个数,再贪心的查找即可得出答案。



#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<vector>
#include<set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;


struct node{
	int a,b,c;
}ans[100005];
int r[100005];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d",&r[i]);
	sort(r,r+n);
	int tempf=0;
	int max1=0,max2=0;
	for(int i=0;i<n;)
	{
		int temp=r[i];
		int times=0;
		while(i<n&&r[i]==temp)
		{
			times++;
			i++;
		}
		if(times>=max1)
		{
			max2=max1;
			max1=times;
		}
		else if(times>max2)
		{
			max2=times;
		}
		tempf++;
	}
	int ansf=0;
	ansf=n/3;
	if((n-max1)/2<ansf)ansf=(n-max1)/2;
	if((n-max1-max2)<ansf)ansf=n-max1-max2;
	int ff=0;
	while(ff<ansf)
	{
		ans[ff].a=r[ff];
		ff++;
	}
	for(int i=0;i<ansf;i++)
	{
		while(r[ff]<=ans[i].a)ff++;
		ans[i].b=r[ff];
		ff++;
	}
	for(int i=0;i<ansf;i++)
	{
		while(r[ff]<=ans[i].b)ff++;
		ans[i].c=r[ff];
		ff++;
	}
	printf("%d\n",ansf);
	for(int i=0;i<ansf;i++)printf("%d %d %d\n",ans[i].c,ans[i].b,ans[i].a);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值