1017. Queueing at Bank——优先队列

其实可以不用优先队列的! 优先队列需要重载“<” ⊙﹏⊙b汗

#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
using namespace std; 

const int maxn =10003;
class node
{
public:
	int h,m,s,pro;
   
};
 bool operator<(const node &a,const node &b) //重载“<” 理解的话 和cmp一样的理解
 {
	 if(a.h ==b.h &&a.m ==b.m ) return a.s >b.s   ;
	 if(a.h ==b.h ) return a.m >b.m ;
	 return a.h>b.h ;
}
node cus[maxn];
priority_queue<node> q;
int n,k,cnt=0;
bool IS(const node a)
{
	if(a.h >17) return false;
	if(a.h ==17&&a.m >0) return false;
	if(a.h ==17&&a.m ==0&&a.s >0) return false;
	return true;
}
int cmp(const void *a,const void *b)
{
	node *c=(node *)a;
	node *d=(node *)b;
	if(c->h ==d->h &&c->m ==d->m ) return c->s -d->s;
	if(c->h ==d->h ) return c->m -d->m ;
	return c->h -d->h; 
	return -1;
}
void Init()
{
	int i;
	scanf("%d%d",&n,&k);
	node a;
	for(i=0;i<n;i++){
		scanf("%d:%d:%d",&a.h ,&a.m ,&a.s );
		scanf("%d",&a.pro );
		if(IS(a)){
			if(a.pro >60) a.pro =60;
			cus[cnt].h =a.h ;cus[cnt].m =a.m ;cus[cnt].s =a.s ;cus[cnt++].pro =a.pro ;
		}
	}
	a.h =8;a.m =0;a.s =0;
	for(i=0;i<k;i++){
		q.push (a);
	}
	qsort(cus,cnt,sizeof(cus[0]),cmp);
}
int getsec( node a, node b)
{
	int s=0;
	if(a.s >=b.s ) s+=(a.s -b.s );
	else {
		a.m -=1;s+=(a.s +60-b.s );
	}
	if(a.m >=b.m ) s+=(a.m -b.m )*60;
	else {
		a.h -=1;s+=(a.m +60-b.m )*60;
	}
	s+=(a.h -b.h )*3600;
	return s;
}
node getb(node a,node b)
{
	node newb;
	newb.s =a.s ;newb.h =a.h ;newb.m =a.m ;
	newb.m +=b.pro  ;
	if(newb.m >=60) {
		newb.m -=60;newb.h +=1;
	}
	return newb;
}
bool low(node a,node b)
{
	if(a.h <b.h )return true;
	if(a.h ==b.h &&a.m <b.m )return true;
	if(a.h ==b.h &&a.m ==b.m &&a.s <b.s )return true;
	return false;
}
void solve()
{
	int total_second=0;
	for(int i=0;i<cnt;i++){
		node a=q.top ();q.pop ();
		if(low(cus[i],a)) {
			total_second+=getsec(a,cus[i]);
			node b=getb(a,cus[i]);
			q.push (b);
		}	
		else {
			node b=getb(cus[i],cus[i]);
			q.push (b);
		}	
	}
	printf("%.1f\n",total_second/(cnt*60.0));
}
int main()
{
	Init();
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值