Wikioi 1246 heap or greedy

Source: Internet
Author: User

Description Description

For a given prime number set S = {p1, p2,..., PK },
To consider the set of all prime factors that belong to the number of S. This set includes P1, P1P2, p1p1, and p1p2p3 (and others ). This is an ugly number set of input S.
Note: we do not think 1 is an ugly number.
Your job is to search for the nth ugly number in the input set S. Longint (signed 32-bit) is sufficient for the program.

Input description Input description

Row 1st: two integers separated by space: K and N, 1 <= k <= 100, 1 <= n <= 100,000.
Row 2nd: K space-separated integers: Elements of the Set S

Output description Output description

For a single row, write the nth ugly number of input S.

Sample Input Sample Input

4 19
2 3 5 7

Sample output Sample output

27


At the beginning, I started to use the little top heap, because it was done in a stupid way, and some people actually passed. However, I did not actually use t because I used the priority queue and then used map, so it was no wonder t, but I don't know where to optimize it. Finally, I had to be greedy. Sorry.

Greedy AC code:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<bitset>#define INF 100007using namespace std;typedef long long ll;typedef unsigned long long ull;ll a[1000010];int b[110],vis[105];int main(){    int k,n,i;    cin>>k>>n;    for(i=1; i<=k; i++)        scanf("%d",b+i);    sort(b+1,b+k+1);    a[0]=1;    for(i=1; i<=n+1; i++)    {        while(1)        {            ll Min=0x7fffffff;            int j,ii;            for(j=1; j<=k; j++)                if(Min>a[vis[j]]*b[j])                    Min=a[vis[j]]*b[j],ii=j;            vis[ii]++;            if(Min!=a[i-1]) {a[i]=Min;break;}        }    }    cout<<a[n]<<endl;    return 0;}

Heap timeout code: the code may be re-submitted after optimization.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<bitset>#define INF 100007using namespace std;typedef long long ll;typedef unsigned long long ull;priority_queue<ll,vector<ll>,greater<ll> >heap;map<ll,int>Map;int main(){    int k,n,i,cnt=-1;    ll m,a[105],s;    scanf("%d%d",&k,&n);    for(i=0;i<k;i++)        scanf("%lld",a+i);    heap.push(1);    while(cnt<n)    {        cnt++;        m=heap.top();heap.pop();        for(i=0;i<k;i++)        {            s=m*a[i];            if(!Map[s]&&s<(1LL<<31LL))                heap.push(s),Map[s]=1;        }    }    printf("%d\n",m);    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.