基于最小推进的Blah数集(poj)

开始用优先队列+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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值