2017ACM/ICPC广西邀请赛 1007 Duizi and Shunzi
Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total
Submission(s): 0 Accepted Submission(s): 0
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)
题解:使用数组保存了每一个数字出现的次数
然后成1开始 循环到n 1ms的时间就可以了
dp记录的是前面循序的长度dp=0,1,2 ans记录的是答案 当你到了第i的数的时候 分以下情况
1》如果数据中没有i,也就是b[i]=0;那么把dp置0就好了
2》如果数据中有 然后dp=2了 那么不管有多少个i 我拿去一个i和前面的i-1,i-2放一起,形成一对 是最优的 (这你要明白)
3》如果数据中有 然后dp=0,1 要是i的数目是奇数 那就多出来的那个i 就和i-1放一起 dp++ 不然的话 dp=0 ans+=b[i]/2;
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <algorithm> 5 #include <cstring> 6 #include <math.h> 7 using namespace std; 8 int b[1000010]; 9 //int qyh[10010],hyh[10010]; 10 int main() 11 { 12 int a,n; 13 while(~scanf("%d",&n)) 14 { 15 for(int i=0; i<1000010; ++i) 16 { 17 b[i]=0; 18 } 19 for(int i=0; i<n; ++i) 20 { 21 scanf("%d",&a); 22 b[a]++; 23 } 24 int dp=0; 25 int ans=0; 26 for(int i=1; i<=n; ++i) 27 { 28 if(b[i]==0) 29 { 30 dp=0; 31 continue; 32 } 33 if(dp==2) 34 { 35 ans+=1; 36 ans+=(b[i]-1)/2; 37 dp=(b[i]-1)%2; 38 } 39 else if(dp==1) 40 { 41 if(b[i]%2==1) 42 { 43 dp++; 44 ans+=b[i]/2; 45 } 46 else 47 { 48 dp=0; 49 ans+=b[i]/2; 50 } 51 } 52 else if(dp==0) 53 { 54 if(b[i]%2==1) 55 { 56 dp++; 57 ans+=b[i]/2; 58 } 59 else 60 { 61 dp=0; 62 ans+=b[i]/2; 63 } 64 } 65 } 66 printf("%d\n",ans); 67 } 68 return 0; 69 }
作者:红雨
出处:https://www.cnblogs.com/52why
微信公众号: 红雨python