有一个集合 S 且 a 是 S 的第一个元素。如果 x 在集合 S 中,则 2x+1 和 3x+1 也都在集合 S 中;除此之外,没有其他元素在集合中
将集合 S 中元素按照升序排列,求第 N 个元素
我的代码超时只有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;
}