日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi

發布時間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> #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; }

轉載于:https://www.cnblogs.com/Yuzao/p/7460107.html

總結

以上是生活随笔為你收集整理的2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。