首页 > 其他 > 详细

BZOJ2456: mode

时间:2018-10-05 18:35:51      阅读:138      评论:0      收藏:0      [点我收藏+]

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

Solution

这题出题人丧心病狂

卡空间卡到炸。不能开数组

你甚至不能用$using\ namespace\ std$和万能头

然后采用$5$个变量,才能卡过去

很神的一个做法,用当前数去抵消不同的数——因为保证这个众数出现次数大于$frac{n}{2}$

#include <cstdio>

int n , tot , t , x ;

int main() {
    scanf( "%d" , &n ) ;
    for( int i = 1 ; i <= n ; i ++ ) {
        scanf( "%d" , &x ) ;
        if( x == t ) tot ++ ;
        else if( !tot ) t = x , tot = 1 ;
        else tot -- ;
    }
    printf( "%d\n" , t ) ;
}

 

BZOJ2456: mode

原文:https://www.cnblogs.com/henry-1202/p/9745312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!