Nike likes playing cards and makes a problem of it.
Now give you n integers, ai(1≤i≤n)ai(1≤i≤n)
We define two identical numbers (eg: 2,22,2) a Duizi,
and three consecutive positive integers (eg: 2,3,42,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)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≤1061≤n≤106).
Then the next line contains n space-separated integers aiai (1≤ai≤n1≤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
贪心。。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
unordered_map<int,int>mp;int n;ll ans=0;
signed main()
{
while(scanf("%d",&n)!=EOF)
{
mp.clear();ans=0;
for(int i=0;i<n;i++){int x;scanf("%d",&x);mp[x]++;}
for(int i=1;i<=n;i++)
{
if(i<=2){ans+=mp[i]/2;mp[i]%=2;}
else{if(mp[i]&&mp[i-1]&&mp[i-2]){mp[i]--;mp[i-1]--;mp[i-2]--;ans++;}ans+=mp[i]/2;mp[i]%=2;}
}
printf("%lld\n",ans);
}
return 0;
}
本文介绍了一个基于贪心算法的卡片组合优化问题,旨在利用给定的整数集合形成尽可能多的顺子和对子,每个数字只能使用一次。通过遍历整数集合并运用贪心策略,算法有效地计算出了最大顺子和对子的数量。
827

被折叠的 条评论
为什么被折叠?



