【Bzoj2456】mode 题解

【Bzoj2456】 mode 题解

【题意】

找出一个数列中出现次数超过n div 2次的数

【思路】

这题看起来简单,实际更简单(雾

一眼看过去,sort就行,但是空间只有一兆,数组肯定没法开,这题其实只要把每个数和一个与其不同的数相抵相抵消。

【代码】

//O(n)
#include

using namespace std;

define rep(i,a,b) for(register int i=(a);i<=(b);++i)

define fileio(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);

inline int read(int &N){
int f=1,x=0;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do{x=x10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
N=f
x;
}

int main(){
register int n,x,y,k;
read(n);
rep(i,1,n){
read(k);
k==n?++y:--y;
if(y<=1)x=k;y=1;
}
printf("%d\n",x);
return 0;
}

【补充】

变量少开,头文件一个足矣,吐槽空间(雾

posted @ 2017-08-20 20:47  zhcs  阅读(105)  评论(0)    收藏  举报