Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 514 Accepted Submission(s): 252
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)
Source
题意:给你n个数,问用这n个数,最多能组成多少对子和顺子
解题思路:贪心,记录下每种数字有几个,1和2都直接组成对子,3~n先判断能不能和前两个数组成顺子,然后剩余的组成对子
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, a, cnt[1000009];
int main()
{
while (~scanf("%d", &n))
{
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; i++) scanf("%d", &a), cnt[a]++;
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (i < 3) ans += cnt[i] / 2, cnt[i] %= 2;
else
{
if (cnt[i] && cnt[i - 1] && cnt[i - 2])
{
cnt[i]--, cnt[i - 1]--, cnt[i - 2]--;
ans++;
}
ans += cnt[i] / 2;
cnt[i] %= 2;
}
}
printf("%d\n", ans);
}
return 0;
}