题目链接

\(Description\)

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

\(Solution\)

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

  1. \(now\neq 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\n",now);

	return 0;
}

相关文章:

  • 2021-10-27
  • 2021-11-26
  • 2022-02-16
  • 2022-02-20
  • 2021-10-28
  • 2022-12-23
  • 2021-12-11
猜你喜欢
  • 2022-01-03
  • 2021-05-31
  • 2021-10-10
  • 2021-12-08
  • 2021-12-07
相关资源
相似解决方案