PAT(A) 1017

#include<stdio.h>
#include<stdlib.h>
#define MinPQSize (10)
struct HeapStruct;
        typedef struct HeapStruct *PriorityQueue;

typedef struct bank ElementType;
struct bank{
	int num;
	int lasttime;
} MinData;
        struct HeapStruct
        {
            int Capacity;
            int Size;
            ElementType *Elements;
        };
struct custom{
	int cametime;
	int servetime;	
} usr[10000];
/* START: fig6_0.txt */
        int compare(struct bank a,struct bank b){
        	if(a.lasttime == b.lasttime)
        		return a.num - b.num;
        	else return a.lasttime - b.lasttime;
        }
        int
        IsEmpty( PriorityQueue H )
        {
            return H->Size == 0;
        }

        int
        IsFull( PriorityQueue H )
        {
            return H->Size == H->Capacity;
        }
        PriorityQueue
        Initialize( int MaxElements )
        {
            PriorityQueue H;

/* 1*/      if( MaxElements < MinPQSize )
/* 2*/          perror( "Priority queue size is too small" );

/* 3*/      H = (PriorityQueue)malloc( sizeof( struct HeapStruct ) );
/* 4*/      if( H ==NULL )
/* 5*/          perror( "Out of space!!!" );

            /* Allocate the array plus one extra for sentinel */
/* 6*/      H->Elements = (ElementType*)malloc( ( MaxElements + 1 )
                                    * sizeof( ElementType ) );
/* 7*/      if( H->Elements == NULL )
/* 8*/        perror( "Out of space!!!" );

/* 9*/      H->Capacity = MaxElements;
/*10*/      H->Size = 0;
/*11*/      H->Elements[ 0 ] = MinData;

/*12*/      return H;
        }
        void
        Insert( ElementType X, PriorityQueue H )
        {
            int i;

            if( IsFull( H ) )
            {
                perror( "Priority queue is full" );
                return;
            }

            for( i = ++H->Size; compare(H->Elements[ i / 2 ] , X)>0; i /= 2 )
                H->Elements[ i ] = H->Elements[ i / 2 ];
            H->Elements[ i ] = X;
        }
/* END */

/* START: fig6_12.txt */
        ElementType
        DeleteMin( PriorityQueue H )
        {
            int i, Child;
            ElementType MinElement, LastElement;

/* 1*/      if( IsEmpty( H ) )
            {
/* 2*/          perror( "Priority queue is empty" );
/* 3*/          return H->Elements[ 0 ];
            }
/* 4*/      MinElement = H->Elements[ 1 ];
/* 5*/      LastElement = H->Elements[ H->Size-- ];

/* 6*/      for( i = 1; i * 2 <= H->Size; i = Child )
            {
                /* Find smaller child */
/* 7*/          Child = i * 2;
/* 8*/          if( Child != H->Size && compare(H->Elements[ Child + 1 ],H->Elements[ Child ])<0 )
/*10*/              Child++;

                /* Percolate one level */
/*11*/          if( compare(LastElement,H->Elements[ Child ])>0 )
/*12*/              H->Elements[ i ] = H->Elements[ Child ];
                else
/*13*/              break;
            }
/*14*/      H->Elements[ i ] = LastElement;
/*15*/      return MinElement;
        }
void swamp(struct custom *a,struct custom *b){
	struct custom tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}
struct custom median3(struct custom a[],int begin,int end){
	int mid;
	mid = (begin + end)/2;
	if(a[begin].cametime>a[mid].cametime)
		swamp(&a[begin],&a[mid]);
	if(a[begin].cametime>a[end].cametime)
		swamp(&a[begin],&a[end]);
	if(a[mid].cametime>a[end].cametime)
		swamp(&a[mid],&a[end]);
	swamp(&a[mid],&a[end-1]);
	return a[end-1];
}
void Insertsort(struct custom a[],int n){
	int i,j;
	struct custom tmp;
	for(i = 1;i<n;i++){
		tmp = a[i];
		for(j = i;tmp.cametime<a[j-1].cametime&&j>0;j--)
			a[j] = a[j-1];
		a[j] = tmp;
	}
}
void partition(struct custom a[],int begin,int end){
	int i,j;
	struct custom pivot;
	if(end - begin>4){
		pivot = median3(a,begin,end);
		i = 0;
		j = end-1;
		while(1){
			while(a[++i].cametime<pivot.cametime);
			while(a[--j].cametime>pivot.cametime);
			if(i<j)
				swamp(&a[i],&a[j]);
			else break;
		}
		swamp(&a[i],&a[end-1]);
		partition(a,begin,i-1);
		partition(a,i+1,end);
	}
	else Insertsort(a + begin,end - begin + 1);  //the array start from a + begin 
}
void quicksort(struct custom a[],int n){
	partition(a,0,n-1);
}
int main(){
	int n,k,hour,min,sec,times,sum,servetime;
	PriorityQueue h;
	struct bank *win,window;

	fscanf(stdin,"%d%d",&n,&k);
	MinData.num = -1;
	MinData.lasttime = -1;
	h = Initialize(k+10);
	win = (struct bank *)malloc(sizeof(struct bank)*k);
	int i;
	for(i = 0;i<k;i++){
		win[i].lasttime = 8*3600;
		win[i].num = i;
		Insert(win[i],h);
	}
	for(i = 0;i<n;i++){
		fscanf(stdin,"%d:%d:%d%d",&hour,&min,&sec,&servetime);
		times = hour*3600+min*60+sec;
		if(servetime>60)
			servetime = 60;
			usr[i].cametime = times;
			usr[i].servetime = servetime*60;

	}
	quicksort(usr,n);
//	for(i = 0;i<n;i++)
//	printf("%d ",usr[i].cametime);
	sum = 0;
	int flag = 0;
	for(i = 0;i<n;i++){
		if(usr[i].cametime>17*3600)
		{
			flag++;
			continue;
		}
		window = DeleteMin(h);
		if(window.lasttime > usr[i].cametime){
			sum += window.lasttime - usr[i].cametime;
			window.lasttime += usr[i].servetime;
		}
		else window.lasttime = usr[i].cametime + usr[i].servetime;
		Insert(window,h);
	}
	printf("%.1lf",sum*1.0/(60*(n-flag)));
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值