bzoj2456 mode

来源:互联网 发布:手机上怎样装修淘宝 编辑:程序博客网 时间:2024/06/05 04:40

题目

出现超过一半的数。

我们每次取出两个数,如果不相等,就扔掉。最后留下来的数就是答案。剩下的就是细节处理了。

#include<cstdio>int n,cnt,tmp,ans,i;char c;inline int read(){    tmp=0;c=getchar();    while(c<'0'||c>'9')c=getchar();    while(c<='9'&&c>='0')tmp=tmp*10+c-'0',c=getchar();    return tmp;}int main(){    n=read();    tmp=read();cnt=1;    for(i=2;i<=n;i+=2)    {        tmp=read();        if(cnt==0)        {            cnt=1;            ans=tmp;            continue;        }        if(ans==tmp)cnt++;        else cnt--;        if(i<n)        {            tmp=read();            if(cnt==0)            {                cnt=1;                ans=tmp;                continue;            }            if(ans==tmp)cnt++;            else cnt--;                 }    }    printf("%d",ans);    return 0;}

还循环展开了一层,跑了200ms,真不知道前面那些几十ms怎么写的233。

原创粉丝点击