Problem Description
Nike likes playing cards and makes a problem of it.
Now give you n integers, ai(1≤i≤n)
We define two identical numbers (eg: 2,2 ) a Duizi,
and three consecutive positive integers (eg: 2,3,4 ) a Shunzi.
Now you want to use these integers to form Shunzi and Duizi as many as possible.
Let s be the total number of the Shunzi and the Duizi you formed.
Try to calculate max(s) .
Each number can be used only once.
Now give you n integers, ai(1≤i≤n)
We define two identical numbers (eg: 2,2 ) a Duizi,
and three consecutive positive integers (eg: 2,3,4 ) a Shunzi.
Now you want to use these integers to form Shunzi and Duizi as many as possible.
Let s be the total number of the Shunzi and the Duizi you formed.
Try to calculate max(s) .
Each number can be used only once.
Input
The input contains several test cases.
For each test case, the first line contains one integer n( 1≤n≤106 ).
Then the next line contains n space-separated integers ai ( 1≤ai≤n )
For each test case, the first line contains one integer n( 1≤n≤106 ).
Then the next line contains n space-separated integers ai ( 1≤ai≤n )
Output
For each test case, output the answer in a line.
Sample Input
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
Sample Output
2 4 3 2HintCase 1(1,2,3)(4,5,6) Case 2(1,2,3)(1,1)(2,2)(3,3) Case 3(2,2)(3,3)(3,3) Case 4(1,2,3)(3,4,5)
给你一串数字,求能得到的顺子数和对子数的最大和。
思路:
排序贪心取,能取就直接取。
//
// main.cpp
// 1007
//
// Created by zc on 2017/9/6.
// Copyright © 2017年 zc. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1100000;
int n,a[N],b[N];
int main(int argc, const char * argv[]) {
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
int m=0,ans=0;
for(int i=0;i<n;i++)
{
if(m>0&&b[m]==a[i]) ans++,m--;
else if(m>1&&b[m]==a[i]-1&&b[m-1]==a[i]-2) ans++,m-=2;
else b[++m]=a[i];
}
printf("%d\n",ans);
}
}