首页 > 代码库 > BZOJ 2456 杂题 卡内存
BZOJ 2456 杂题 卡内存
2456: mode
Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 3702 Solved: 1551
[Submit][Status][Discuss]
Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。
Input
第1行一个正整数n。
第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。
Sample Input
5
3 2 3 1 3
3 2 3 1 3
Sample Output
3
HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。
zju2132 The Most Frequent Number
内存限制只有1M开了两个库就烂了
题目很水 求众数而且众数的个数大于n/2
所以就把不相同的两个数都消掉 一样的就累加起来 剩下的那个数就是众数
代码如下:
#include<cstdio>#define For(i,x,y) for(int i=x;i<=y;++i)using namespace std;int pre,pnum;int main(){ int n;scanf("%d",&n);int x; For(i,1,n) { scanf("%d",&x); if(pnum) { if(x==pre)pnum++; else pnum--; } else { pre=x; pnum=1; } } printf("%d",pre);}
BZOJ 2456 杂题 卡内存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。