超级简单站外题求助
查看原帖
超级简单站外题求助
865625
KobeBeanBryantCox楼主2023/10/3 21:53

有一个集合 SS 且 aa 是 SS 的第一个元素。如果 xx 在集合 SS 中,则 2x+12x+1 和 3x+13x+1 也都在集合 SS 中;除此之外,没有其他元素在集合中

将集合 SS 中元素按照升序排列,求第 NN 个元素


我的代码超时只有70分,请问各位大佬有什么高级思路吗。时空限:1 Sec,128 MB

#include<bits/stdc++.h>
#define Code using
#define by namespace
#define wjb std
Code by wjb;
void out(int x)
{
	if(x<0)putchar('-'),x=-x;
	if(x<10)putchar(x+'0');
	else out(x/10),putchar(x%10+'0');
}
priority_queue<int,vector<int>,greater<int> >q;
map<int,bool>f;
int main()
{
	int n,a;
	while(scanf("%d%d",&a,&n)!=EOF)
	{
		f.clear(),q=priority_queue<int,vector<int>,greater<int> >();
		q.push(a),f[a]=true;
		for(int i=1;i<n;i++)
		{
			if(!f[q.top()*2+1])f[q.top()*2+1]=true,q.push(q.top()*2+1);
			if(!f[q.top()*3+1])f[q.top()*3+1]=true,q.push(q.top()*3+1);
			q.pop();
		}
		out(q.top()),putchar('\n');
	}
	return 0;
}
2023/10/3 21:53
加载中...