Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2266 Accepted Submission(s): 895
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.
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
Hint
Case 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)
Source
问题简述:(略)
问题分析:
这个问题是算对子和顺子的数量,用贪心法来解决。
首先要排序,桶排序对于后续的计算比较方便。
然后,顺序算对子和顺子数量即可。
程序说明:
排序的结果,前后多个0对于计算对子和顺子的代码起到简化作用。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* HDU6188 Duizi and Shunzi */
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 1e6;
int cnt[N + 2];
int main()
{
int n, a;
while(~scanf("%d", &n)) {
memset(cnt, 0, sizeof(int) * (n + 2));
// 桶排序
for(int i = 1; i <= n; i++) {
scanf("%d", &a);
cnt[a]++;
}
long long ans = 0;
for(int i = 1; i <= n; i++) {
// 先算对子数量
ans += cnt[i] / 2;
cnt[i] %= 2;
// 计算顺子数量
if(cnt[i - 1] && cnt[i] && cnt[i + 1]) {
ans++;
cnt[i- 1]--;
cnt[i]--;
cnt[i + 1]--;
}
}
printf("%lld\n", ans);
}
return 0;
}