TYVJ3680 找妹子

时间: 1000ms / 空间: 1200KiB / Java类名: Main

背景

本题由 @fjzzq2002 提供,已奖励20金币。

描述

sps是zzq的好伙伴。

sps一天叫来了许多个妹子。然后sps看了看这些妹子,说了m个数。这m个数中出现次数最多的数就是sps最喜欢的妹子的编号。因为sps非常专一,他最喜欢的妹子的编号出现的次数大于m的一半。

你自然想知道一下sps最喜欢哪个妹子。

m<=1000000。m个数均在int范围内的正数。

注意看时空限制!

输入格式

第一行一个数m。

第二行m个数。

输出格式

输出出现次数最多的数。

备注

空间1.2MB,连m个数都存不下。

样例输入:

8
2 3 3 2 3 3 2 3

样例输出:

3

 

题目钦定了答案值出现次数大于m/2

在线处理,记当前答案ans,答案出现次数cnt,每次读入数x,如果x和ans相同,cnt++。不同,cnt–-。当cnt<0时,将ans更新为x。如果一个数出现次数大于m/2,它最终一定会被选为ans。

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 int read(){
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 int x,cnt,m;
15 int ans;
16 int main(){
17     m=read();
18     int i,j;
19     for(i=1;i<=m;i++){
20         x=read();
21         if(x==ans){
22             cnt++;
23             continue;
24         }
25         cnt--;
26         if(cnt<0){
27             ans=x;
28             cnt=1;
29         }
30     }
31     cout<<ans<<endl;
32     return 0;
33 }

 

posted @ 2016-09-26 18:28  SilverNebula  阅读(219)  评论(0编辑  收藏  举报
AmazingCounters.com