【每日一题(9)】Duizi and Shunzi HDU

来源:互联网 发布:液晶电视怎么连接网络 编辑:程序博客网 时间:2024/05/16 01:17

Duizi and Shunzi HDU - 6188

Nike likes playing cards and makes a problem of it.

Now give you n integers, ai(1≤i≤n)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 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

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)

题意

题目大意:每两个同样的数字可以组成对子,每三个连续的数字可以组成顺子
于是,我们可以通过栈或者数组来解决这个问题

1 1 2 3 我们可以组成 (1 1) 2 3 或者 1(1 2 3);都是一个,并没有区别
1 2 2 3 我门可以组成 1 (2 2) 3 或者 2 (1 2 3);都是一个,并没有区别
1 2 3 3 我们可以组成 1 2 (3 3) 或者 (1 2 3) 3;对于这种情况而言,组成对子,一定是一种可能,而组成顺子时,3这个数字可能可以和后面出现的数组成更多对子或顺子;

在这题中,我会写出我理解中的两种做法,一种是数组,一种是栈

题解

数组解法

#include<cstdio>#include<string.h>#include<iostream>int main(void){    char a[1000500];    int n,i,x;    while(scanf("%d",&n) != EOF)    {        memset(a,0,sizeof(a));        int duizi = 0,shunzi = 0;        for(i = 1;i <= n; i++)        {            scanf("%d",&x);            a[x]++;        }        for(i = 1;i <= 1000500; i++)        {            if(a[i] >= 2)            {                duizi = a[i]/2 + duizi;                a[i] = a[i]%2;            }            if(i < 1000498)            {                if(a[i] == 1 && a[i + 1]%2 == 1 && a[i + 2] != 0)                {                    shunzi++;                    a[i]--;                    a[i + 1]--;                    a[i + 2]--;                }            }        }        printf("%d\n",shunzi + duizi);    }    return 0;}

栈的解法

#include<iostream>#include<cstdio>#include<stack>#include<queue>using namespace std;struct node{    int x;    friend bool operator < (node a, node b)    {      return a.x > b.x;    }};int main(void){  int n;  while(~scanf("%d",&n)){      stack<int>s;      node i;      priority_queue<node> q;      int k = n;      int a,duizi = 0,shunzi = 0;      while(k--){        scanf("%d",&a);        i.x = a;        q.push(i);      }      while(n--){        int a;        node i;        i = q.top();        a = i.x;        q.pop();        if(!s.empty()){          if(a == s.top()){            s.pop();            duizi++;          }else if(!s.empty() && a == (s.top() + 1)){            s.pop();            if(!s.empty() && a == (s.top() + 2)){              s.pop();              shunzi++;            }else{              s.push(a - 1);              s.push(a);            }          }else{            s.push(a);          }        }else{          s.push(a);        }      }      printf("%d\n",shunzi + duizi);  }  return 0;}
原创粉丝点击