【水】【卡内存】【bzoj 2456】mode

来源:互联网 发布:惊天破仔仔换头像软件 编辑:程序博客网 时间:2024/05/19 11:48

2456: mode

Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 2157  Solved: 910

Description

给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。

Input

第1行一个正整数n。
第2行n个正整数用空格隔开。

Output

一行一个正整数表示那个众数。

Sample Input

53 2 3 1 3

Sample Output

3

HINT

100%的数据,n<=500000,数列中每个数<=maxlongint。

题解:

卡内存题。
因为这个数会出现超过n/2次,所以每次让两个不同的数抵消,最后剩下的一定是这个数了。

Code:

#include<cstdio> using namespace std;int main(){    int n,x,ans=-1,s=1; scanf("%d",&n);    for (int i=1; i<=n; i++){        scanf("%d",&x);        if (x==ans) s++;        else s--;        if (!s) ans=x,s=1;    }    printf("%d\n",ans);    return 0;}
0 0
原创粉丝点击