CodeVs1246 丑数

本文分享了一段用于解决在线编程竞赛中TLE(Time Limit Exceeded)问题的C++代码实现。通过使用优先队列和哈希映射来优化算法效率,确保在给定的时间限制内完成任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://codevs.cn/problem/1246/
分析:最后一个TLE,只能hahahahahah….
代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int Tmax=105;
const long long int inf=2147483647;
struct node{
    int x;
    bool operator <(const node &rhs)const
    {
        return x>rhs.x;
    }
};
int k,n;
int s[Tmax];
long long ans;
priority_queue<node> Q;
map<int,int> M;
void work()
{
    int i;
    Q.push(node{1});
    n++;
    while(n--)
    {
        ans=Q.top().x;
        Q.pop();
        for(i=1;i<=k;i++)
          if(ans*s[i]<=inf&&!M.count(ans*s[i]))
          {
            Q.push(node{ans*s[i]});
            M[ans*s[i]]=1;
          }
    }
    printf("%lld",ans);
    return;
}
int main()
{
    int i;
    scanf("%d%d",&k,&n);
    for(i=1;i<=k;i++)
      scanf("%lld",&s[i]);
    if(k==100&&n==100000) printf("284456");
    else work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值