哈理工oj 2223水题 【优先队列】

来源:互联网 发布:淘宝双11如何抢购 编辑:程序博客网 时间:2024/06/05 17:42
水题Time Limit: 500 MSMemory Limit: 32768 KTotal Submit: 407(138 users)Total Accepted: 153(99 users)Rating: Special Judge: NoDescription

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

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

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

Input

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

每组数据

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

第二行N个整数,第i个数字t[i](<=10000)代表第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小桥流水哗啦啦,我和小岛去偷瓜~
#include<bits/stdc++.h>using namespace std;int a[100005];bool cmp(int a, int b){    return a > b;}int main(){    int N;    cin >> N;    while(N--)    {        int n, m;        cin >> 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++)            cin >> a[i];        sort(a, a + n, cmp);        for(int i = 0; i < n; i++)        {            int x = q.top();            q.pop();            x += a[i];            q.push(x);        }        int ans = -1111;        while(!q.empty())        {            if(ans < q.top())                ans = q.top();            q.pop();        }        cout << ans << endl;    }    return 0;}

阅读全文
0 0
原创粉丝点击