[BZOJ 2456]mode

danihao123 posted @ 2016年1月03日 13:20 in 题解 with tags bzoj 神奇题 , 735 阅读
转载请注明出处:http://danihao123.is-programmer.com/

原来这就是BT众数题的始祖啊~

首先讲一下方法(这种方法其实叫摩尔投票法):

喜闻乐见的代码来辣!本来还想写输入输出优化的呢。

/**************************************************************
    Problem: 2456
    User: danihao123
    Language: C++
    Result: Accepted
    Time:388 ms
    Memory:804 kb
****************************************************************/
 
#include <cstdio>
int main(){
    register int cnt=0,ans,i;
    int n,k;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&k);
        if(!cnt){
            ans=k;
            ++cnt;
            continue;
        }
        if(k==ans){
            ++cnt;
        }else{
            --cnt;
        }
    }
    printf("%d\n",ans);
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter