bzoj 2456: mode(找众数)
2016-09-17 14:24
351 查看
2456: mode
Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 3607 Solved: 1515
[Submit][Status][Discuss]
Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。Input
第1行一个正整数n。第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。Sample Input
53 2 3 1 3
Sample Output
3HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。思路:对两两不相等的数进行抵消,则最后剩下的数则是该众数! 用num记录当前抵消后剩下的数,当num=0的时候把该数变为tmp,不等则cnt--,相等则cnt++
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int tot = 1 , n , num , tem; scanf("%d%d",&n,&num); for(int i = 2 ; i <= n ; i++) { scanf("%d",&tem); if(tot == 0) { tot = 1; num = tem; continue; } if(num == tem) tot++; else tot--; } printf("%d\n",num); return 0; }
相关文章推荐
- vagrant 环境配置以及常用命令
- 解决MySQL ERROR 1130 (HY000): Host 'XXXX' is not allowed to connect to this MySQL server
- 媒体查询-手机尺寸参考
- 爬虫, 从python2 到 python3
- 如何划分man文档的章节
- python脚本退出后 不应该为负值
- ImageLoader 介绍
- mac中配置hadoop常见问题
- python脚本退出后 不应该为负值
- P1896 [SCOI2005]互不侵犯King
- javascript checkbox作为开关,控制全选和全不选的动态关联实现
- Android菜单选项
- PHP数据库基础操作
- 另类电商年收入上亿美金
- 给 Android 开发者的 RxJava 详解
- LAMP平台下搭建论坛和博客系统
- 系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式(转)
- Xshell ssh长时间连接不掉线设置
- GNU复合语句
- Linux系统上获取命令帮助信息的方法