HDU6188

Duizi and Shunzi

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 153    Accepted Submission(s): 71

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.

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)

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)

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

 1 //2017-08-31
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 const int N = 1100000;
10 int arr[N], n;
11 bool book[N];
12
13 int main()
14 {
15     //freopen("input1007.txt", "r", stdin);
16     while(scanf("%d", &n) != EOF){
17         for(int i = 0; i < n; i++)
18               scanf("%d", &arr[i]);
19         sort(arr, arr+n);
20         memset(book, 0, sizeof(book));
21         int ans = 0;
22         for(int i = 1; i < n; i++){
23             if(i >= 2){
24                 int p1 = -1, p2 = -1;
25                 for(int j = i-1; j >= 0; j--){
26                     if(arr[j] == arr[i]-1 && !book[j]){
27                         p1 = j;
28                     }
29                     if(arr[j] == arr[i]-2 && !book[j]){
30                         p2 = j;
31                         break;
32                     }
33                     if(arr[j] < arr[i]-1)break;
34                 }
35                 if(p1 != -1 && p2 != -1){
36                     ans++;
37                     book[i] = book[p1] = book[p2] = 1;
38                 }
39             }
40             if(arr[i-1] == arr[i] && !book[i-1] && !book[i]){
41                 ans++;
42                 book[i-1] = book[i] = 1;
43             }
44         }
45         printf("%d\n", ans);
46     }
47
48     return 0;
49 }
时间: 2024-12-29 12:32:47

HDU6188的相关文章

hdu6188 Duizi and Shunzi (贪心或者dp)

题意 有n张牌,第i张牌上的数字是a[i].我们定义 两张数字是一样的牌 为对子.我们定义 三张数字连续的牌 为顺子.我们想把这n张牌组成尽可能多的顺子和对子.请计算并输出能组成的最多的顺子和对子的数量. 分析 我是傻逼我是傻逼我是傻逼!重要的事情说三遍!! 这是一道贪心,而且是并不是很复杂的贪心,但是我在场上坚定的认为他是个dp然后连写带调两个小时才过掉它! 结束后我在网上查了一下,果然只有我这么傻逼,但是我还是想把这个奇怪的dp思路写下来··· 我们定义f[i]为前i张牌中对子和顺子最多的数