哈理工OJ 2223 水题(用到了优先队列)

本题用到了优先队列,关于优先队列的定义,大家可以去查询资料,在这里就不多做解释了。
下面附上AC代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;//参考的代码,学会了优先队列的使用以及逆推思想 

int cmp(int x,int y)
{
    return x>y;
}
int a[100005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        priority_queue<int,vector<int>,greater<int> >q;
        for(int i=0;i<m;i++)
        {
            q.push(0);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)
        {
            int temp=q.top();
            q.pop();
            temp+=a[i];
            q.push(temp);
        }
        int ans=-1;
        while(!q.empty())
        {
            if(ans<q.top())
            {
                ans=q.top();
            }
            q.pop();
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(优先队列)