ICPC2017南宁邀请赛1004&&HDU6197 (贪心
2017-09-19 13:58
176 查看
Duizi and Shunzi
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
题意
给一组数,问最多可以组成多少对子(1,1)或者顺子(1,2,3)我们可以这样想,首先能组成对子一定要先组成对子(按照贪心原理,对子比顺子少一张牌),然后在看是否可以组成一个顺子 但是 第2张牌 必须是奇数 因为是偶数的话 自身就可以做组成对子,这样再加上第三张牌 就相比组成对子多了一份付出,但是第三张牌就无所谓了。
AC代码
#include <bits/stdc++.h> using namespace std; #define LL long long #define CLR(a,b) memset(a,(b),sizeof(a)) const int MAXM = 1e3+10; const int MAXN = 1e6+10; int num[MAXN]; int main() { int n; while(~scanf("%d",&n)) { int x; CLR(num,0); for(int i = 0; i < n; i++) { scanf("%d",&x); num[x]++; } int ans = 0; for(int i = 0; i <= MAXN; i++) { if(num[i] >= 2) { ans += num[i] / 2; num[i] %= 2; } if(num[i] && num[i+1]&1 && num[i+2]) { ans++; num[i]--; num[i+1]--; num[i+2]--; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- ICPC2017南宁邀请赛1005&&HDU6185 (矩阵快速幂/黑科技
- 2017 ACM/ICPC Asia Regional Shenyang Online 1004 & hdu6197:array array array
- ICPC2017南宁邀请赛1005&&HDU6197 (模拟
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题 离散化+贪心
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation(贪心问题+区间覆盖)
- 计蒜客 Frequent Subsets Problem&&2017 Icpc南宁赛
- BZOJ 4017&&2017 icpc 西安G 区间异或和
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem (状态压缩)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Train Seats Reservation
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Frequent Subsets Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
- HDU-6186 CS Course (线段树)(2017ACM/ICPC广西邀请赛)
- HDU 5135 Little Zu Chongzhi's Triangles(贪心||状压dp)2014ICPC 广州站现场赛
- HDU - 6205 card card card (贪心)2017 ACM/ICPC Asia Regional Shenyang Online
- BZOJ 4017&&2017 icpc 西安G 区间异或和
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- 2017 ACM-ICPC南宁网络赛: J. Minimum Distance in a Star Graph(BFS)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem 最长不下降序列
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛M题