
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
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
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
Sample Input
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
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
//2017-08-31
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int arr[N], n;
bool book[N]; int main()
{
//freopen("input1007.txt", "r", stdin);
while(scanf("%d", &n) != EOF){
for(int i = ; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr+n);
memset(book, , sizeof(book));
int ans = ;
for(int i = ; i < n; i++){
if(i >= ){
int p1 = -, p2 = -;
for(int j = i-; j >= ; j--){
if(arr[j] == arr[i]- && !book[j]){
p1 = j;
}
if(arr[j] == arr[i]- && !book[j]){
p2 = j;
break;
}
if(arr[j] < arr[i]-)break;
}
if(p1 != - && p2 != -){
ans++;
book[i] = book[p1] = book[p2] = ;
}
}
if(arr[i-] == arr[i] && !book[i-] && !book[i]){
ans++;
book[i-] = book[i] = ;
}
}
printf("%d\n", ans);
} return ;
}