又是毕业季2

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/cggwz https://blog.csdn.net/cggwz/article/details/83386429

传送门

枚举答案,检查是当前数倍数的数的个数
还有一条性质,那就是,随着人数的递增,所得答案递减。
所以从上往下枚举答案即可。
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int vis[1000005];
int ans[1000005];
int up;
int check(int x){
	if(ans[x]>0){
		return ans[x];
	}
	int y=x;
	while(y<=up){
		ans[x]+=vis[y];
		y+=x;
	}
	return ans[x];
}
int main(){
	int top=0;
	scanf("%d",&n);
	memset(vis,0,sizeof(vis));
	for(int i=0;i<n;i++){
		int x;
		scanf("%d",&x);
		vis[x]++;
		top=max(top,x);
	}
	up=top;
	memset(ans,0,sizeof(ans));
	for(int i=1;i<=n;i++){
		while(top>1&&check(top)<i){
			top--;
		}
		printf("%d\n",top);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cggwz/article/details/83386429