#include<stdio.h> #include<iostream> #include<stdlib.h>//malloc? #define ERROR 0 #define MAX 10000 using namespace std; //定义元素类型customer struct customer{ int time_arrive;//以秒计 同下 int time_done;// int time_processing;// int time_remain;//余下时间,开始=processing }; int cmp ( const void *a , const void *b ) { return (*(struct customer *)a).time_arrive >(*(struct customer *)b).time_arrive?1:-1; } bool winava(struct customer* win[],int k){ for(int i=0;i<k;i++){ if(win[i]==NULL) return true; } return false; } int MyMin(struct customer* win[],int k){//当窗口都没有人时返回最大值 int min=60*61; while(k--){ if(win[k]!=NULL&&min>win[k]->time_remain) min=win[k]->time_remain; } return min; } bool clear(struct customer* win[],int k){ for(int i=0;i<k;i++) if(win[i]!=NULL) return 0; return 1; } int main(){ int n,k,axis=28800,h,m,s,process,order=0;//n:cus,k:windows order记录以处理的客户 0开始 scanf("%d%d",&n,&k); struct customer *cus=(struct customer*)malloc(sizeof(struct customer)*k); for(int i=0;i<n;i++){ scanf("%d:%d:%d",&h,&m,&s); scanf("%d",&process); if(60*60*h+m*60+s>61200){//晚于17点 n--; i--; continue; } cus[i].time_arrive=60*60*h+m*60+s;//以秒计时 cus[i].time_remain=cus[i].time_processing=process*60; cus[i].time_done=0; } qsort(cus,n,sizeof(struct customer),cmp);//按到达时间排序 struct customer* win[k]; for(int i=0;i<k;i++) win[i]=NULL; while(!(order==n&&clear(win,k))){//order==n时表示队列空 int min=MyMin(win,k); if(order!=n&&(cus[order].time_arrive-axis<min)&&winava(win,k)){//队首到达-axis<min(窗口)&&窗口有空 order==n时表示队列空 出队 for(int i=0;i<k;i++){ if(win[i]!=NULL){ win[i]->time_remain-=(cus[order].time_arrive-axis); } } axis=cus[order].time_arrive; for(int i=0;i<k;i++){ if(win[i]==NULL){ win[i]=&cus[order]; break; } } order++; } else{//处理一个 axis+=min; for(int i=0;i<k;i++){ if(win[i]!=NULL&&min==win[i]->time_remain){ win[i]->time_done=axis; cout<<win[i]->time_done-win[i]->time_arrive<<endl; win[i]=NULL; } else if(win[i]!=NULL){ win[i]->time_remain-=min; } } } } int sum=0; for(int i=0;i<n;i++){ sum+=cus[i].time_done-cus[i].time_arrive-cus[i].time_processing; } double ave=sum/n; printf("%0.1f",ave/60); return 0; }
时间: 2024-10-05 20:14:31