水题——优先队列

Description
因为是有关于接水的问题,便简称为水题了(。

N个人排队在M个出水口前接水,第i个人接水需时为t[i],

请问接水的最短用时是多少?

Input
第一行一个整数 T ,代表有 T 组数据。

每组数据

第一行两个整数 N(<=100000) , M(<=10000) 代表有 N 个人 M 个出水口。

第二行N个整数,第i个数字ti代表第i个人接水用时t[i]。

Output
对于每组数据输出一个整数,代表所需的最少接水时间。
Sample Input
2

5 3

1 2 3 4 5

6 3

1 2 3 3 4 5

Sample Output
5

6

Hint
小桥流水哗啦啦,我和小岛去偷瓜~。

下面是AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int lyf(int x,int y)//从大到小排序
{
    return x>y;
}
int main()
{
    int t,n,m,a[10005],i;
    scanf("%d",&t);
    while(t--)
    {
        priority_queue<int,vector<int>,greater<int> >q;
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
        {
            q.push(0);//每个出水口进入队列时接水时长初始化为0
        }
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n,lyf);
        int p;
        for(i=0;i<n;i++)
        {
            p=q.top();//根据优先队列特点取出最小时长
            q.pop();
            p=p+a[i];
            q.push(p);//将累加后的时长压入队列
        }
        int h=-1;
        int s;
        while(!q.empty())//取队列中最大时长作为最少的接水时间
           {
                s=q.top();
                q.pop();
                    {
                        if(s>h)
                        {
                            h=s;
                        }
                    }
           }
           printf("%d\n",h);
    }
}

题目传送门:水题(点击可交题)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值