(HDU 6188)Duizi and Shunzi

Duizi and Shunzi

Time limit3000 ms
Memory limit32768 kB
OSWindows

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

代码如下

ime577ms
Memory5128kB
Length819
LangC
#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值