【每日一题(9)】Duizi and Shunzi HDU
来源:互联网 发布:液晶电视怎么连接网络 编辑:程序博客网 时间:2024/05/16 01:17
Duizi and Shunzi HDU - 6188
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,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 aiai (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)
题意
题目大意:每两个同样的数字可以组成对子,每三个连续的数字可以组成顺子
于是,我们可以通过栈或者数组来解决这个问题1 1 2 3 我们可以组成 (1 1) 2 3 或者 1(1 2 3);都是一个,并没有区别
1 2 2 3 我门可以组成 1 (2 2) 3 或者 2 (1 2 3);都是一个,并没有区别
1 2 3 3 我们可以组成 1 2 (3 3) 或者 (1 2 3) 3;对于这种情况而言,组成对子,一定是一种可能,而组成顺子时,3这个数字可能可以和后面出现的数组成更多对子或顺子;在这题中,我会写出我理解中的两种做法,一种是数组,一种是栈
题解
数组解法
#include<cstdio>#include<string.h>#include<iostream>int main(void){ char a[1000500]; int n,i,x; while(scanf("%d",&n) != EOF) { memset(a,0,sizeof(a)); int duizi = 0,shunzi = 0; for(i = 1;i <= n; i++) { scanf("%d",&x); a[x]++; } for(i = 1;i <= 1000500; i++) { if(a[i] >= 2) { duizi = a[i]/2 + duizi; a[i] = a[i]%2; } if(i < 1000498) { if(a[i] == 1 && a[i + 1]%2 == 1 && a[i + 2] != 0) { shunzi++; a[i]--; a[i + 1]--; a[i + 2]--; } } } printf("%d\n",shunzi + duizi); } return 0;}
栈的解法
#include<iostream>#include<cstdio>#include<stack>#include<queue>using namespace std;struct node{ int x; friend bool operator < (node a, node b) { return a.x > b.x; }};int main(void){ int n; while(~scanf("%d",&n)){ stack<int>s; node i; priority_queue<node> q; int k = n; int a,duizi = 0,shunzi = 0; while(k--){ scanf("%d",&a); i.x = a; q.push(i); } while(n--){ int a; node i; i = q.top(); a = i.x; q.pop(); if(!s.empty()){ if(a == s.top()){ s.pop(); duizi++; }else if(!s.empty() && a == (s.top() + 1)){ s.pop(); if(!s.empty() && a == (s.top() + 2)){ s.pop(); shunzi++; }else{ s.push(a - 1); s.push(a); } }else{ s.push(a); } }else{ s.push(a); } } printf("%d\n",shunzi + duizi); } return 0;}
- 【每日一题(9)】Duizi and Shunzi HDU
- Duizi and Shunzi HDU
- HDU-6188:Duizi and Shunzi
- HDU 6188 Duizi and Shunzi 贪心
- Hdu 6188 Duizi and Shunzi【思维+Dp】
- HDU -- 6188 Duizi and Shunzi 【思维】
- HDU 6188 Duizi and Shunzi(贪心)
- HDU-6188 Duizi and Shunzi(思维)
- HDU-6188 Duizi and Shunzi 贪心
- HDU6188-Duizi and Shunzi
- hdu6188 Duizi and Shunzi
- HDU-6188-Duizi and Shunzi(广州邀请赛G题)(简单贪心)
- HDU 6188 && 2017广西邀请赛:Duizi and Shunzi
- HDU 6188 Duizi and Shunzi(贪心+细节)
- 【2017广西邀请赛】hdu 6188 Duizi and Shunzi 贪心
- 广西 2017 邀请赛 Duizi and Shunzi
- 2017广西邀请赛 Duizi and Shunzi(贪心+DP)
- HDU6188 | 2017广西邀请赛 Duizi and Shunzi (贪心)
- sql mybatis
- jar包,war包和tar包的区别
- linux实现彩色进度条,rpm和yum安装的区别和进程的task_struck结构
- CSAPP第三章学习笔记(二)
- Java开发-Java中代理设计模式的实现
- 【每日一题(9)】Duizi and Shunzi HDU
- LeetCode:Best Time to Buy and Sell Stock with Transaction Fee
- 【每日一题(10)】Hero HDU-4310
- 抖动
- 平时工作中一定会用到的Linux指令(每周会持续更新~)
- md5加密
- Android自定义View实现自动无限轮播,点击图片进入详情页
- 高数-二重积分
- STM32学习笔记之USB虚拟串口描述符简介