大一集训--c++优先队列priority_queue

优先队列

题目一:P1537

#include <bits/stdc++.h>

using namespace std;
struct _people
{
    int num;
    int t;
};
struct cmp1
{
    bool operator()(_people a,_people b)
    {
        if(a.t!=b.t)
            return a.t>b.t;
        else
            return a.num>b.num;
    }
};
int main()
{
    int n;
    struct _people peo;
    priority_queue<_people,vector<_people>,cmp1>que;
    queue<_people>SAVE;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        peo.num=i;
        cin >> peo.t;
        que.push(peo);
    }
    for(int i=1;i<=n;i++)
    {
        if(i==1)
        {
            cout << que.top().num;
        }
        else
        {
            cout << " " << que.top().num;
        }
        SAVE.push(que.top());
        que.pop();
    }
    cout << endl ;
    double all=0;
    while(SAVE.size()>1)
    {
        all+=SAVE.front().t;
        int save;
        save=SAVE.front().t;
        SAVE.pop();
        SAVE.front().t+=save;
    }
    double ave;
    ave=all/n;
    printf("%.2lf\n",ave);
    return 0;
}

题目二:P1688

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int x;
    priority_queue<int,vector<int>,greater<int> >que;
    cin >> n;
    while(n--)
    {
        cin >> x;
        que.push(x);
    }
    int ans=0;
    while(que.size()>1)
    {
        int t;
        t=que.top();
        que.pop();
        t+=que.top();
        que.pop();
        que.push(t);
        ans+=t;
    }
    cout << ans << endl ;
    return 0;
}

题目三:P1689

#include <bits/stdc++.h>

using namespace std;
int a[400005];
int b[400005];
struct _sum
{
    int ai;
    int bi;
    long long sum;
};
struct cmp1
{
    bool operator ()(_sum a,_sum b)
    {
        return a.sum>b.sum;
    }
};
int main()
{
    int n;
    scanf("%d",&n);
    priority_queue<_sum,vector<_sum>,cmp1>que;
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&b[i]);
    }
    struct _sum save;
    for(int i=1;i<=n;i++)
    {
        save.ai=i;
        save.bi=1;
        save.sum=a[i]+b[1];
        que.push(save);
    }
    for(int i=1;i<=n;i++)
    {
        printf("%lld\n",que.top().sum);
        struct _sum add;
        add.ai=que.top().ai;
        add.bi=que.top().bi+1;
        add.sum=a[que.top().ai]+b[que.top().bi+1];
        que.push(add);
        que.pop();
    }
    return 0;
}

题目四:P355

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int x;
    while(cin >> n)
    {
        priority_queue<int,vector<int>,greater<int> >que;
        while(n--)
        {
            cin >> x;
            que.push(x);
        }
        int ans=0;
        while(que.size()>1)
        {
            int t;
            t=que.top();
            que.pop();
            t+=que.top();
            que.pop();
            que.push(t);
            ans+=t;
        }
        cout << ans << endl ;
    }
    return 0;
}

题目五:P1692

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        priority_queue<int,vector<int>,greater<int> >que;
        for(int i=1;i<=n;i++)
        {
            int dox;
            int x;
            cin >> dox;
            if(dox==1)
            {
                cin >> x;
                que.push(x);
            }
            else if(dox==2)
            {
                cout << que.top() << endl ;
            }
            else if(dox==3)
            {
                que.pop();
            }
        }
    }
    return 0;
}

题目六:P1691

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int x;
    priority_queue<int,vector<int>,greater<int> >que;
    cin >> n;
    while(n--)
    {
        cin >> x;
        que.push(x);
    }
    long long ans=0;
    while(que.size()>1)
    {
        int t;
        t=que.top();
        que.pop();
        t+=que.top();
        que.pop();
        que.push(t);
        ans+=t;
    }
    cout << ans << endl ;
    return 0;
}

题目七:P1690

#include <bits/stdc++.h>

using namespace std;
struct _people
{
    int num;
    int t;
    int period;
};
struct cmp1
{
    bool operator()(_people a,_people b)
    {
        if(a.t!=b.t)
            return a.t>b.t;
        else
            return a.num>b.num;
    }
};
int main()
{
    priority_queue<_people,vector<_people>,cmp1>que;
    string s;
    int nnum;
    int tt;
    int i=0;
    while(cin >> s)
    {
        if(s[0]=='#')
            break;
        cin >> nnum >> tt;
        que.push({nnum,tt,tt});
    }
    int k;
    cin >> k;
    struct _people change;
    while(k--)
    {
        cout << que.top().num << endl;
        change=que.top();
        que.pop();
        change.t+=change.period;
        que.push(change);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值