题目传送门//res tp hdu

已知必定存在一个元素出现次数超过一半,考虑用栈

若当前元素等于栈顶元素,入栈,反之出栈,并将当前元素入栈

最终的栈顶元素即是所求

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i = (a);i>=(b);--i)
#define fo(i,a,b) for(int i =(a);i<(b);++i)
#define de(x) cout<<#x<<" = "<<x<<endl;
#define endl '\n'
#define ls(p) ((p)<<1)
#define rs(p) (((p)<<1)|1)
using namespace std;
typedef long long ll;
const int mn = 1e6+10;
int n;
ll Top,t;
int cnt;
// stack
int main(){
while(scanf("%d",&n)!=EOF){
cnt = 1;
scanf("%lld",&Top);
rep(i,2,n){
scanf("%lld",&t);
if(t == Top)++cnt;
else{
if(cnt == 1)
Top = t;
else{
--cnt;
}
}
}
printf("%lld\n",Top); } }

hdu 1029 求出现次数过半的数的更多相关文章

  1. hdu 5038 求出现次数最多的grade

    http://acm.hdu.edu.cn/showproblem.php?pid=5038 模拟水题 求出现次数最多的grade.如果有多个grade出现的次数一样多,且还有其他的grade,则把这 ...

  2. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  4. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  5. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  6. HDU 1029 基础dp

    题目链接:Ignatius and the Princess IV 大意:就是在N个数里找出唯一一个至少出现过(N+1)/ 2 次的数. 1 <= N <= 999999. hash: / ...

  7. hdu1568&&hdu3117 求斐波那契数前四位和后四位

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...

  8. hdu 1029 Ignatius and the Princess IV(排序)

    题意:求出现次数>=(N+1)/2的数 思路:排序后,输出第(N+1)/2个数 #include<iostream> #include<stdio.h> #include ...

  9. Openjudge 1.13-28:出现次数超过一半的数

    总时间限制:  1000ms 内存限制:  65536kB 描述 给出一个含有n(0 < n <= 1000)个整数的数组,请找出其中出现次数超过一半的数. 数组中的数大于-50且小于50 ...

随机推荐

  1. access函数

    access函数是按照实际用户ID和实际组ID进行访问测试的.函数的定义如下: #include <unistd.h> int access(const char* pathname, i ...

  2. zabbix之自定义告警

    zabbix支持内置的告警类型.email,sms,等 有时候需要自定义类型的. [其他微信,钉钉都差不多方式,只是脚本不一样] 自定义告警类型[自定义邮件] 编写自定义脚本,并测试成功. [脚本需要 ...

  3. python pywin32 安装

    pip install pywin32 参考: https://blog.csdn.net/qq_38161040/article/details/85075158

  4. shell 统计行数,单词个数,字符个数

    如果我们想知道1.txt中有多少行,多少个单词,多少个字符.我们可以使用wc命令.选项与参数-l:今列出行-w:今列出多少字(英文单词)-m:多少字符[zhang@localhost ~]$ cat ...

  5. Java 生成六位短信验证码

    在<Java 生成三位随机数>中,简要介绍了使用Java生成三位随机数的方法,前几天在工作中遇到生成6位短信验证码的需求,验证码由6位随机数字构成,不包含字母.6位随机数通常用作短信验证码 ...

  6. Linux快速编译

    #include<bits/stdc++.h> using namespace std; string name, tmp, s; int main() { cin >> tm ...

  7. pwn学习日记Day9 基础知识积累

    知识杂项 libc是Linux下的ANSI C的函数库. LOOKUP函数 数组形式:公式为= LOOKUP(lookup_value,array) 式中 array-包含文本.数字或逻辑值的单元格区 ...

  8. java经典算法题50道

    原文 JAVA经典算法50题[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序 ...

  9. chip based learning

    chip types Transistor mode of operation Digital chip: 0/1  -> digital clac Analog chip: sound / b ...

  10. Linux中查看系统资源占用情况的命令

    用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...