您的位置:首页 > 其它

ICPC2017南宁邀请赛1004&&HDU6197 (贪心

2017-09-19 13:58 176 查看

Duizi and Shunzi

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.

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)

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
2


题意

给一组数,问最多可以组成多少对子(1,1)或者顺子(1,2,3)

我们可以这样想,首先能组成对子一定要先组成对子(按照贪心原理,对子比顺子少一张牌),然后在看是否可以组成一个顺子 但是 第2张牌 必须是奇数 因为是偶数的话 自身就可以做组成对子,这样再加上第三张牌 就相比组成对子多了一份付出,但是第三张牌就无所谓了。

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e6+10;

int num[MAXN];

int main()
{
int n;
while(~scanf("%d",&n)) {
int x;
CLR(num,0);
for(int i = 0; i < n; i++) {
scanf("%d",&x); num[x]++;
}
int ans = 0;
for(int i = 0; i <= MAXN; i++) {
if(num[i] >= 2) {
ans += num[i] / 2;
num[i] %= 2;
}
if(num[i] && num[i+1]&1 && num[i+2]) {
ans++;
num[i]--; num[i+1]--; num[i+2]--;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐