E Pair and Straight

E Pair and Straight
时间限制 内存限制 出题人 3 Second 32768 kB 王湧博
题目描述
人生当苦无妨,良人当归即好。 —《雪中悍刀行》
我们现在有很多很多很多数字,现在我们想知道这些数字里面有最多有多少个 P 与 S。
我们定义 P的含义为大小相同的两个数字的个数,例如一组数字里面有两个1 那么这两个1就 是一个P,四个1,那么就是2个P。 S的含义为连续的是三个数字,例如1,2,3就是一个S。
还请acmer 请将可以得到的最多的P与S输出
输入 第1行输入T(1 ≤ T ≤ 20)组数据 第2行输入n(1 ≤ n ≤ 1e5) 第3行输入n个数字x(1 ≤ x ≤ n),
输出
输出最多的 P + S
输入样例
4 7 1 2 3 4 5 6 7 9 1 1 1 2 2 2 3 3 3 6 2 2 3 3 3 3 6 1 2 3 3 4 5
输出样例
2 4 3 2

//贪心,用最少的资源,得到最大的结果。
//先考虑 p,后考虑s; 
#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
map<int,int> mmp;
int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int i,n,x;
		mmp.clear();
		scanf("%d",&n);
		for (i=0;i<n;i++)
		{
			scanf("%d",&x);
			mmp[x]++;
		}
		int cnt = 0;
		for (i=1;i<=n;i++)
		{
			if (mmp[i]>=2)
			{
				cnt += mmp[i]/2;
				mmp[i] %= 2;
			}
			if (mmp[i] && mmp[i+1]%2 && mmp[i+2])
			{
				cnt++;
				mmp[i]--;
				mmp[i+1]--;
				mmp[i+2]--;
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/81176446