栈。

将数字排序后,一个一个压入栈。如果栈顶两个元素形成了对子,那么$ans+1$,弹出栈顶两个元素;如果栈顶三个元素形成了顺子,那么$ans+1$,弹出栈顶三个元素。

#include<bits/stdc++.h>
using namespace std; const int maxn = 1000000 + 10;
int n;
int a[maxn];
int b[maxn];
int c[maxn]; int main() {
while(~scanf("%d", &n)) {
memset(a, 0, sizeof a);
for(int i = 1; i <= n; i ++) {
int x;
scanf("%d", &x);
a[x] ++;
}
int ans = 0;
int num = 0;
int top = -1;
for(int i = 1; i <= n; i ++) {
while(a[i] --) {
c[++ num] = i;
}
}
for(int i = 1; i <= n; i ++) {
if(top == -1) {
top ++;
b[top] = c[i];
} else {
if(c[i] == b[top]) {
top --;
ans ++;
} else {
if(top < 1) {
top ++;
b[top] = c[i];
} else {
if(b[top - 1] + 1 == b[top] && b[top] + 1 == c[i]) {
top --;
top --;
ans ++;
} else {
top ++;
b[top] = c[i];
}
}
}
}
}
printf("%d\n", ans);
}
return 0;
}

  

HDU 6188 Duizi and Shunzi的更多相关文章

  1. HDU 6188 Duizi and Shunzi 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续 ...

  2. Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. Duizi and Shunzi HDU

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【hdu6188】Duizi and Shunzi(贪心)

    2017ACM/ICPC广西邀请赛 重现赛1007 Duizi and Shunzi 题意 有n张牌,问你最多能组成多少对子+顺子?一个牌只能用在一个顺子或者对子中. 题解 本来想写dp的,不会..小 ...

  5. 2017ACM/ICPC广西邀请赛 1007 Duizi and Shunzi

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...

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

    Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...

  8. HDU 6188最小费用流

    题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...

  9. hdu6188 Duizi and Shunzi (贪心或者dp)

    题意 有n张牌,第i张牌上的数字是a[i].我们定义 两张数字是一样的牌 为对子.我们定义 三张数字连续的牌 为顺子.我们想把这n张牌组成尽可能多的顺子和对子.请计算并输出能组成的最多的顺子和对子的数 ...

随机推荐

  1. 跟我一起写Makefile(二)

    Makefile 总述——————— 一.Makefile里有什么? Makefile里主要包含了五个东西:显式规则.隐晦规则.变量定义.文件指示和注释. 1.显式规则.显式规则说明了,如何生成一个或 ...

  2. JS函数大全 莫名其妙找到的

    1 .document.write(""); 输出语句 2 .JS中的注释为// 3 .传统的HTML文档顺序是:document->html->(head,body) ...

  3. 将new Date() 格式化为 ’2018-10-11‘ 的字符串格式

    function dateToString( date , format ){ if(!date) return ""; if (!Common.type.isDate(date) ...

  4. 5W次单点修改,求最长的连续上升子序列 HDU 3308

    题目大意:给你n个数,m个操作. 有两种操作: 1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度. 对于每次询问,输出连续最长子序列的长度 思路:用线段树 ...

  5. 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法

    TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  6. 重构改善既有代码设计--重构手法08:Replace Method with Method Object (以函数对象取代函数)

    你有一个大型函数,其中对局部变量的使用,使你无法釆用 Extract Method. 将这个函数放进一个单独对象中,如此一来局部变量就成了对象内的值域(field) 然后你可以在同一个对象中将这个大型 ...

  7. How to reset XiaoMi bluetooth headphone Youth edition.

    To reset the speaker 1. Long press the phone call button to shut off the speaker 2. Connect the char ...

  8. jquery实现简单轮播

    先上简单的html代码 <!DOCTYPE HTML> <html> <head> <link rel="stylesheet" type ...

  9. BERT(Bidirectional Encoder Representations from Transformers)理解

    BERT的新语言表示模型,它代表Transformer的双向编码器表示.与最近的其他语言表示模型不同,BERT旨在通过联合调节所有层中的上下文来预先训练深度双向表示.因此,预训练的BERT表示可以通过 ...

  10. LCD之mipi DSI接口驱动调试流程【转】

    转自:http://blog.csdn.net/liwei16611/article/details/68146912 1.LCD MIPI DSI协议 MIPI-DSI是一种应用于显示技术的串行接口 ...