HDU6188
Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 153 Accepted Submission(s): 71
Problem Description
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
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
Sample Input
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
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)
Source
//2017-08-31
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int arr[N], n;
bool book[N]; int main()
{
//freopen("input1007.txt", "r", stdin);
while(scanf("%d", &n) != EOF){
for(int i = ; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr+n);
memset(book, , sizeof(book));
int ans = ;
for(int i = ; i < n; i++){
if(i >= ){
int p1 = -, p2 = -;
for(int j = i-; j >= ; j--){
if(arr[j] == arr[i]- && !book[j]){
p1 = j;
}
if(arr[j] == arr[i]- && !book[j]){
p2 = j;
break;
}
if(arr[j] < arr[i]-)break;
}
if(p1 != - && p2 != -){
ans++;
book[i] = book[p1] = book[p2] = ;
}
}
if(arr[i-] == arr[i] && !book[i-] && !book[i]){
ans++;
book[i-] = book[i] = ;
}
}
printf("%d\n", ans);
} return ;
}
HDU6188的更多相关文章
- 【hdu6188】Duizi and Shunzi(贪心)
2017ACM/ICPC广西邀请赛 重现赛1007 Duizi and Shunzi 题意 有n张牌,问你最多能组成多少对子+顺子?一个牌只能用在一个顺子或者对子中. 题解 本来想写dp的,不会..小 ...
- hdu6188 Duizi and Shunzi (贪心或者dp)
题意 有n张牌,第i张牌上的数字是a[i].我们定义 两张数字是一样的牌 为对子.我们定义 三张数字连续的牌 为顺子.我们想把这n张牌组成尽可能多的顺子和对子.请计算并输出能组成的最多的顺子和对子的数 ...
- hdu6188&&百度之星初赛(B) T5
度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...
- CF-1110 (2019/02/08)
CF-1110 A. Parity 快速幂的思想,考虑最后一位即可 #include <bits/stdc++.h> using namespace std; typedef long l ...
随机推荐
- 题解 P5091 【【模板】欧拉定理】
欧拉定理:若 \(gcd(a,n)=1\),\(a^{\varphi(n)}\equiv 1(mod\ n)\) 设 \(1\sim n-1\) 中与 \(n\) 互素的 \(\varphi(n)\) ...
- 13_python_内置函数
- hashMap tableSizeFor 实现原理
基于jdk1.8 hashMap实现,要求容量大小是2的整次方,例如:2/4/8/16/32/64/128...,而不能是中间的某个值.这是为什么呢? map是数组+链表的数据结构,读写数据都需要首先 ...
- C#6.0语言规范(十二) 数组
数组是一种数据结构,包含许多通过计算索引访问的变量.包含在数组中的变量(也称为数组的元素)都是相同的类型,这种类型称为数组的元素类型. 数组具有确定与每个数组元素相关联的索引数的等级.数组的等级也称为 ...
- OC中双向链表的实现
双向链表的概念 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都 ...
- eclipse上搭建mybatis
1..在help中打开 2.搜索mybatipse 3:功能简介 1:要查找某一个方法 在dao接口中某一个方法中 按住 Ctrl键 鼠标指到方法名称上 选择open xml 就会自动跳 ...
- Eleasticsearch启动失败问题解决
问题: [root@dnode1 bin]# ./elasticsearch -d [root@dnode1 bin]# Exception in thread "main" ja ...
- 【xsy2815】净空 大暴力
绝了场上居然没做这一题 题目大意:给你一个数$x=\Pi_{i=1}^{n}a_i!$. 你需要将x表示为$x=\Pi_{i=1}^{m}(c_i!)^{d_i}p$ 满足$p$无法再分解,且$(c_ ...
- [LeetCode] 206. Reverse Linked List_Easy tag: Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- ElasticSearch 6.x 父子文档[join]分析
ES6.0以后,索引的type只能有一个,使得父子结构变的不那么清晰,毕竟对于java开发者来说,index->db,type->table的结构比较容易理解. 按照官方的说明,之前一个索 ...