开始用优先队列+set 判重做的 T:就是 丑数的那个代码
#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<vector>
using namespace std;
int main(){
int a,b;
while(scanf("%d%d"),&a,&b){
priority_queue<long long ,vector<long long >,greater<long long > > pq;//优先队列排序
set<long long > st;//集合判重
pq.push(a);
st.insert(a);
for(int i=1;;i++){
if(i==b){
printf("%lld",pq.top());
break;
}
long long x=pq.top();pq.pop();
long long x1=2*x+1;
if(!st.count(x1)){
st.insert(x1);
pq.push(x1);
}
x1=x1+x;
if(!st.count(x1)){
st.insert(x1);
pq.push(x1);
}
}
}
return 0;
}
注,vector set map deque 支持clear();stack queue priority_queue 通过pop() 方法清空;
注,vector deque 支持下标遍历;set map 支持迭代器遍历;stack queue 不支持遍历;
网上找的最小推进算法(每次找到一个满足数,同样可以解决丑数问题trail,还有一个cow clean 的区间覆盖问题flag
#include<iostream>
#include<cstdio>
using namespace std;
long long shz[1000000+10];//tot每增加1生成一个最小数
int head2,head3,tot;
int main(){
int a,b;
while(cin>>a>>b){
shz[1]=a,head2=head3=tot=1;
while(tot<b){
long long t1=(shz[head2]<<1)+1,t2=(shz[head3]<<1)+shz[head3]+1;
if(t1<t2){
shz[++tot]=t1;
head2++;
}
else if(t1==t2){
shz[++tot]=t1;
head2++;
head3++;
}
else if(t1>t2){
shz[++tot]=t2;
head3++;
}
}
printf("%lld\n",shz[tot]);
}
return 0;
}