• bzoj 2456: mode ——独特水法


    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。

    ————————————————————————————————

    这道题内存1MB 有毒qwq

    其实我们可以维护一个大小为1的栈 如果读进来的数和当前栈里面的数不一样 就将它的出现次数减去1

    否则加1 如果它的值被减为0 就换当前数 最后栈里面的数就是答案辣 因为如果是众数的话 最差情况下它也不会被弹出去的

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    const int M=1e6+7,mod=1e5+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int ans,h,n,k;
    int main(){
        n=read();
        for(int i=1;i<=n;i++){
            k=read();
            if(!ans) ans=k,h=1;
            else{
                if(k==ans) h++;
                else{
                    h--;
                    if(!h) ans=k,h=1;
                }
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    网站要满足用户的期望
    在网站内如何引导你的用户
    网站要一步一步的引导用户
    photoshop:制作sprite拼贴图片
    photoshop:css3插件
    注册表修改PSD关联photoshop
    photoshop:找不到增效工具入口点
    ajax:$.get()
    Photoshop支持ico输出
    JavaScript数字精度丢失问题总结
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7772746.html
Copyright © 2020-2023  润新知