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 r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine whatmaximum number of snowmen they can make from those snowballs.
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn(1 ≤ ri ≤ 109). The balls' radii can coincide.
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.
7 1 2 3 4 5 6 7
2 3 2 1 6 5 4
3 2 2 3
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);
}