bzoj2456:mode

很神奇的一道题

内存限制1M 只能开4个int

找众数,用抵消的思想,如果相互能够抵消,无论之前的那个数是众数还是后面的数是众数,要抵消的部分是相同的。如果最后剩下的数 tot=0或者t不是出现次数最多的数,找到的那个数出现的次数就没有>n/2

#include <cstdio>
using namespace std;
int n,x,t,tot;
int main()
{
    scanf("%d", &n);
    while (n--)
    {
        scanf("%d", &x);
        if (tot==0){t=x;tot=1;}
        else if (t==x) tot++;
        else tot--;
    }
    printf("%d", t);
}


你可能感兴趣的:(bzoj2456:mode)