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.
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)
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
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 ", ans); 46 } 47 48 return 0; 49 }