bzoj2456 mode
Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。
Input
第1行一个正整数n。
第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。
Sample Input
5
3 2 3 1 3
3 2 3 1 3
Sample Output
3
HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。
zju2132 The Most Frequent Number
正解:思维题。
这道题太神辣!!!蒟蒻的我完全不会。。
因为众数必定出现$n/2$次以上,所以我们只要把不同的数碰掉,就能找出众数了。具体实现看代码吧。
注意不能开$iostream$这一类的库,否则会$MLE$
1 //It is made by wfj_2048~ 2 #include <stdio.h> 3 #define inf (1<<30) 4 #define il inline 5 #define RG register 6 #define ll long long 7 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) 8 9 using namespace std; 10 11 int n,x,cnt,ans; 12 13 il int gi(){ 14 RG int x=0,q=1; RG char ch=getchar(); 15 while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); 16 if (ch=='-') q=-1,ch=getchar(); 17 while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); 18 return q*x; 19 } 20 21 il void work(){ 22 n=gi(); 23 for (RG int i=1,x;i<=n;++i){ 24 x=gi(); if (!cnt) ans=x; 25 ans==x ? cnt++ : cnt--; 26 } 27 printf("%d\n",ans); return; 28 } 29 30 int main(){ 31 File("mode"); 32 work(); 33 return 0; 34 }