求代码优化
  • 板块学术版
  • 楼主KID2695
  • 当前回复12
  • 已保存回复12
  • 发布时间2023/11/5 16:02
  • 上次更新2023/11/5 16:20:20
查看原帖
求代码优化
544696
KID2695楼主2023/11/5 16:02

查找序列的绝对众数,代码如下。

#include<cstdio>
char *p1,*p2,buf[10005];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,10000,stdin),p1==p2)?EOF:*p1++)
using namespace std;
int lread(){char c;int x=0,y=1;c=gc();while(c<'0'||c>'9'){if(c=='-') y=-1;c=gc();}while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^'0');c=gc();}return x*y;}
int n,x,bj,cnt;
int main(){
	n=lread();
	for(int i=1;i<=n;++i){
		x=lread();
		if(!cnt){
		    bj=x;
		    cnt=1;
		}
		else {
		    if(x==bj) ++cnt;
		    else --cnt;
		}
	}
	printf("%d",bj);
	return 0;
}

开了c++20+O2离最优解还差5ms,求细节优化

2023/11/5 16:02
加载中...