水题 | ||||||
| ||||||
Description | ||||||
因为是有关于接水的问题,便简称为水题了(。 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 | ||||||
小桥流水哗啦啦,我和小岛去偷瓜~。 | ||||||
Source | ||||||
"诚德软件杯"哈尔滨理工大学第四届ACM程序设计团队赛 |
优先队列练习
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct cmp
{
bool operator ()(int a,int b)
{
return a>b;
}
};
bool cmp1(int a,int b)
{
return a>b;
}
int num[1000003];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
priority_queue<int,vector<int>,cmp>q;
cin>>n>>m;
for(int i=1; i<=m; i++)
{
q.push(0);
}
for(int i=1; i<=n; i++)
{
cin>>num[i];
}
sort(num+1,num+n+1,cmp1);
for(int i=1; i<=n; i++)
{
int f=q.top();
f+=num[i];
q.pop();
q.push(f);
}
int ans=-1;
while(!q.empty())
{
if(q.top()>ans)
ans=q.top();
q.pop();
}
cout<<ans<<endl;
}
}