zoukankan      html  css  js  c++  java
  • BZOJ.2456.mode(绝对众数)

    题目链接

    (Description)

    限制空间(只能保留两个变量),求给定n个数中出现次数超过(frac{n}{2})的数。

    (Solution)

    维护两个变量,(now)(cnt);枚举(x)

    1. (now eq a_i):若(cnt=0),则(now=a_i,cnt=1),否则(cnt--)
    2. (now=a_i)(cnt++)
      最后的(now)为答案。因为绝对众数出现次数超过了一半。

    这题更好的方式是出成交互:Majority Element。

    //920kb	56ms
    #include <cstdio>
    #include <cctype>
    //#define gc() getchar()
    #define MAXIN 100000
    #define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
    
    char IN[MAXIN],*SS=IN,*TT=IN;
    
    inline int read()
    {
    	int now=0;register char c=gc();
    	for(;!isdigit(c);c=gc());
    	for(;isdigit(c);now=now*10+c-'0',c=gc());
    	return now;
    }
    
    int main()
    {
    	int n=read(),now,cnt=0,t;
    	while(n--)
    		if((t=read())==now) ++cnt;
    		else if(!cnt) now=t, cnt=1;
    		else --cnt;
    	printf("%d
    ",now);
    
    	return 0;
    }
    
  • 相关阅读:
    POJ-1182 食物链
    hdu 1879 继续畅通工程
    HDU 2604 Queuing
    hdu 1232 畅通工程
    POJ-1611 The Suspects
    Free DIY Tour
    Tr A
    不容易系列之(3)―― LELE的RPG难题
    W3C标准冒泡、捕获机制
    JavaScript 浏览器事件解读
  • 原文地址:https://www.cnblogs.com/SovietPower/p/9392651.html
Copyright © 2011-2022 走看看