解题思路
一开始想过贪心,用 t/吃一个的时间,算出每个人能吃多少个整的,然后发现不行,因为当一个同学拿下一块时,不知道是否还有剩余,要知道谁先拿也很麻烦。
所以考虑——暴力吧!
直接用一个优先队列模拟同学吃巧克力的过程,把一个 p a i r pair pair丢到小根堆中,第一关键字为吃完这一个所到的时间,第二关键字为小朋友编号,每次取出栈顶 t o p top top,若 t o p . f i r s t < = t , m − − top.first<=t,m-- top.first<=t,m−−,然后把 ( t o p . x + a [ t o p . y ] , t o p . y ) (top.x+a[top.y],top.y) (top.x+a[top.y],top.y)加入堆中。
若 t o p . f i r s t > t top.first>t top.first>t,那当 x . f i r s t − a [ x . s e c o n d ] < t x.first-a[x.second]<t x.first−a[x.second]<t时,这个巧克力就是被吃过但没吃完
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
pair<int,int>x;
int T,n,m,t,s,ans1,ans2,a[110];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
int main() {
scanf("%d",&T);
while(T--) {
while(!q.empty())q.pop();
ans1=0,ans2=0;
scanf("%d%d%d",&n,&m,&t);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1; i<=n; i++)
if(m>0) {
m--;
q.push(make_pair(a[i],i));
}
while(!q.empty()) {
x=q.top();
q.pop();
if(x.first<=t) {
//cout<<x.first<<" "<<x.second<<" "<<i<<endl;
ans1++;
ans2++;
if(m>0) {
m--;
q.push(make_pair(x.first+a[x.second],x.second));
}
} else if(x.first-a[x.second]<t)
ans2++;
}
printf("%d %d\n",ans1,ans2-ans1);
}
}