Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.
Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) - the total number of customers, and K (<=100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.
Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.
Output Specification:
For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.
写了好几个小时,考试的时候怎么办,时间不够啊
#include <stdio.h> #include <stdlib.h> #include <algorithm> using namespace std; typedef struct Cust { int hh,mm,ss,p; Cust(){} Cust( int a,int b,int c,int d) { hh=a; mm=b; ss=c; p=d; } }Cust; typedef struct Wind { int hh,mm,ss; Wind(){} Wind( int a,int b,int c) { hh=a; mm=b; ss=c; } }Wind; int Compare (Cust *cust,int i,Wind *wind,int j); int Account (Cust *cust,int i,Wind *wind,int j); bool cmp1(Cust a,Cust b); bool cmp2(Wind a,Wind b); int main () { int num,w; scanf("%d %d",&num,&w); int i,a,b,c,d,right=0; Cust * cust=new Cust[num]; Wind * wind=new Wind[w]; for( i=0;i<num;i++) { scanf("%d:%d:%d %d",&a,&b,&c,&d); if(a<17) { cust[right++]=Cust(a,b,c,d); } } for( i=0;i<w;i++) wind[i]=Wind(8,0,0); ///////////////////////////////// sort(cust,cust+right,cmp1); int amount=0; for( i=0;i<right;i++) { if( Compare(cust,i,wind,0)<0) { //请求时间小于窗口空闲时间 amount+=Account(cust,i,wind,0); wind[0].mm+=cust[i].p; if( wind[0].mm>=60) { wind[0].mm-=60; wind[0].hh++; } //printf("%d ",amount); } else { wind[0].ss=cust[i].ss; wind[0].mm=cust[i].p+cust[i].mm; wind[0].hh=cust[i].hh; if( wind[0].mm>=60) { wind[0].mm-=60; wind[0].hh++; } } sort(wind,wind+w,cmp2); } printf("%.1f\n",amount/60.0/right); system("pause"); return 0; } int Compare (Cust *cust,int i,Wind *wind,int j) { if( cust[i].hh!=wind[j].hh) return cust[i].hh-wind[j].hh; else if( cust[i].mm!=wind[j].mm) return cust[i].mm-wind[j].mm; else return cust[i].ss-wind[j].ss; } int Account (Cust *cust,int i,Wind *wind,int j) { int a=0,b=0; a=cust[i].hh*3600+cust[i].mm*60+cust[i].ss; b=wind[j].hh*3600+wind[j].mm*60+wind[j].ss; /* Cust temp=cust[i]; while( temp.hh<wind[i].hh||temp.mm<wind[i].mm||temp.ss<wind[i].ss) { temp.ss++; a++; if( temp.ss==60) { temp.ss=0; temp.mm++; } if( temp.mm==60) { temp.mm=0; temp.hh++; } }*/ return b-a; } bool cmp1(Cust a,Cust b) { if( a.hh!=b.hh) return a.hh<b.hh; else if( a.mm!=b.mm) return a.mm<b.mm; else return a.ss<b.ss; } bool cmp2(Wind a,Wind b) { if( a.hh!=b.hh) return a.hh<b.hh; else if( a.mm!=b.mm) return a.mm<b.mm; else return a.ss<b.ss; }
原文地址:http://blog.csdn.net/lchinam/article/details/44005969