bzoj2456: mode

用抵消的思想好神啊这个。

#include<cstdio>
int n,ans,cnt,u;
int main(){
    scanf("%d",&n);
    while(n--){
        scanf("%d",&u);
        if(!cnt) ans=u,cnt=1;
        else if(u==ans) cnt++;
        else cnt--;
    }
    printf("%d\n",ans);
    return 0;
}

  

2456: mode

Time Limit: 1 Sec  Memory Limit: 1 MB
Submit: 3594  Solved: 1510
[Submit][Status][Discuss]

Description

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

Input

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

Output

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

Sample Input

5
3 2 3 1 3

Sample Output

3

HINT

 

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


 


zju2132 The Most Frequent Number

 

Source

[Submit][Status][Discuss]
posted @ 2016-09-10 14:34  BBChq  阅读(94)  评论(0编辑  收藏  举报