Duizi and Shunzi
Time limit | 3000 ms |
---|---|
Memory limit | 32768 kB |
OS | Windows |
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 ≤10^6).
Then the next line contains n space-separated integers aiai (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
耐克喜欢玩扑克牌,但也有问题。现在给你n个整数,ai(1≤i≤n)
我们定义两个相同的数字(例如:2,2)一个对子,和三个连续的正整数(例如:2,3,4)一个顺子
现在您要使用这些整数来尽可能多地形成顺子和对子。让我们来看看你所形成的顺子和对子的总数
尝试计算最大值
每个号码只能使用一次
- 输入
输入包含几个测试用例
对于每个测试用例,第一行包含一个整数n(1≤n≤10^6)
然后下一行包含n个空格分隔整数ai ai(1≤ai≤n)
- 产量
对于每个测试用例,将答案输出到一行中。
- 样例输入
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
- 样例输出
2
4
3
2
代码如下
ime | 577ms |
---|---|
Memory | 5128kB |
Length | 819 |
Lang | C |
#include <stdio.h>
#include <string.h>
int card[1000005];
int main(int argc, char const *argv[]) {
int n, j, sum;
while (~scanf("%d", &n)) {
memset(card, 0, sizeof(card));
sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &j);
card[j] ++;
}
for (int i = 1; i <= n; i++) {
if (i >= 3 && card[i]%2==0 && card[i-1] && card[i-2] && card[i] && card[i+1]%2 && card[i+2]) {
card[i-2] --;
card[i-1] --;
card[i] -= 2;
card[i+1] --;
card[i+2] --;
sum += 2;
}
while (card[i] >= 2) {
card[i] -= 2;
sum ++;/*然后写博客的时候我队友说这里其实直接sum += card/2 然后 card%2就可以,更省时间*/
}
if(i >= 3 && card[i] && card[i-1] && card[i-2]) {
card[i-2] --;
card[i-1] --;
card[i] --;
sum ++;
}/*这里其实能看出来和第一个的情况有一些相似,所以其实三个三个判断就可以..*/
}/*但昨天改了好多版,这个是第一个ac的所以就贴这个8*/
printf("%d\n", sum);
}
return 0;
}