Duizi and ShunziTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2377 Accepted Submission(s): 933 Problem Description Nike likes playing cards and makes a problem of it. Input The input contains several test cases. Output For each test case, output the answer in a line. Sample Input
扫描二维码关注公众号,回复:
2503668 查看本文章
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) |
题意:
大致就是说两个相同的数字组成对子,三个连续的数字组成顺子,问最大组成对子数加顺子数的数量;
思路:
一道典型的贪心题,思路就是对于每个数看他和下一个能否构成对子,剩下的又是否能与第三个构成顺子;ps:由于数据量的关系还可以直接用数组存走捷径省时间(首发用的map就TLE了,大概是我写的太麻烦了)
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = (int)1e6 + 5;
int num[maxn];
int main()
{
int n,d;
while (~scanf("%d",&n))
{
memset(num,0,sizeof(num));
int ans = 0;
for (int i = 0;i < n;i ++)
{
scanf("%d",&d);
num[d] ++;
}
for (int i = 1;i <= n - 2;i ++)
{
ans += num[i] / 2;
num[i] %= 2;
if (num[i] && num[i + 1] & 1 && num[i + 2])
{
ans ++;
num[i + 1] --;
num[i + 2] --;
}
}
ans += num[n - 1] / 2 + num[n] / 2;
printf("%d\n",ans);
}
return 0;
}