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)题目大意:有两种操作:可以用三张连续的牌组成顺子,可以用两张相同的牌组成对子,记对子和顺子的和为S,求Max(s)
解题报告:这题考场没写出来,第一步很容易想到,那就是把多出来的对子先打掉,这样显然更优(虽然此时可以组成顺子,但是组成对子是等价的,不会影响答案),现在每一张牌就只剩下1或2张了,考虑剩下的牌怎么打..
然后我就挂在了这里,天真的以为直接能打顺子就打掉,打完即可,然后挂在了手make的1 1 2 3 3 这组上,此时考试已经结束了..考后发现这里是需要DP的,对于剩一张牌的我们不需要决策,能用到就用到.剩两张牌需要做决策,显然是可以选择打一个对子或两个顺子,显然两个顺子是更好的,所以能打两个顺子就打掉,不能就打对子.#include#include #include #include #include #include #define RG register#define il inline#define iter iterator#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;const int N=1e6+5;int t[N],n,f[N];void work(){ int x,m=0,ans=0; for(RG int i=0;i<=n;i++)f[i]=0,t[i]=0; for(int i=1;i<=n;i++){ scanf("%d",&x); t[x]++;m=Max(x,m); } for(int i=1;i<=m;i++){ if(t[i]>2){ ans+=t[i]>>1; if(t[i]%2==0)ans--; if(t[i]%2)t[i]=1; else t[i]=2; } } f[1]=t[1]>>1;if(t[1]>1)t[1]=0; f[2]=f[1]+(t[2]>>1);if(t[2]>1)t[2]=0; for(int i=3;i<=m;i++){ f[i]=f[i-1]; if(!t[i])continue; if(t[i]==2) f[i]=f[i-1]+1; if(t[i-1] && t[i-2] && f[i-2]+1>=f[i]){ f[i]=f[i-2]+1;t[i]--;t[i-1]--;t[i-2]--; } else{ if((t[i-1] && t[i-2] && f[i]>f[i-2]+1) || !t[i-1] || !t[i-2]){ if(t[i]==2)t[i]=0;//如果不能打出两个顺子,那么直接打对子 } } } printf("%d\n",ans+f[m]);}int main(){ while(~scanf("%d",&n))work(); return 0;}