hdu 6188

题意:给出一个数列,问能组成对子和顺子的最大数目

思路:我们当然知道优先组成对子,但是有 1 2 3 3 4 5的话,就不适应了,所以得判断下该种情况

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 2e6;
 5 
 6 int n,x,H[N];
 7 
 8 int main( ){
 9     while(scanf("%d",&n)!=EOF) {
10         for(int i = 1; i <= n; ++i) H[i] = 0;
11         for(int i = 1; i <= n; ++i) scanf("%d",&x),H[x] += 1;
12         int ans = 0;
13         for(int i = 1; i <= n; ++i) {
14             if(!H[i]) continue;
15             if(i-1>=1 && i-2 >= 1 && H[i-1] && H[i-2])
16                 ans += 1,H[i] -= 1,H[i-1]-=1,H[i-2] -= 1;
17             ans += H[i] / 2,H[i] = H[i] % 2;
18         }
19         printf("%d
",ans);
20     }
21     return 0;
22 }
原文地址:https://www.cnblogs.com/hhxj/p/7459808.html