【51 Nod】3241 小明和他的同学们【堆】

12 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述


解题思路

一开始想过贪心,用 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<=tm,然后把 ( 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.firsta[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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值