We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>#include<queue>#include<vector>#include<algorithm>usingnamespacestd;structcustomer{longlongarrTime;longlongcookTime;longlongindex;};boolcompare(customer&a,customer&b){returna.arrTime<b.arrTime;}structCompare1{booloperator()(customera,customerb){returna.cookTime>b.cookTime;}};structCompare2{booloperator()(customera,customerb){returna.arrTime>b.arrTime;}};intmain(){vector<customer>c;vector<bool>done;longlongn;cin>>n;for(longlongi=0;i<n;i++){customercust;cin>>cust.arrTime;cin>>cust.cookTime;cust.index=i;c.push_back(cust);done.push_back(false);}priority_queue<customer,vector<customer>,Compare2>arrivalList;for(longlongi=0;i<n;i++)arrivalList.push(c[i]);priority_queue<customer,vector<customer>,Compare1>waitList;vector<longlong>tat(n);vector<longlong>ct(n);//next step- priority queue work startslonglongcount=0;longlongtotalTime=0;while(count!=n){while(!arrivalList.empty()&&arrivalList.top().arrTime<=totalTime){waitList.push(arrivalList.top());arrivalList.pop();}customernext;if(!waitList.empty()){next=waitList.top();//cout<<"Job "<<next.index<<endl;waitList.pop();totalTime+=next.cookTime;ct[next.index]=totalTime;done[next.index]=true;count++;}elseif(!arrivalList.empty()){next=arrivalList.top();//cout<<"Job "<<next.index<<endl;arrivalList.pop();totalTime+=next.cookTime;ct[next.index]=totalTime;done[next.index]=true;count++;}}longlongsum=0;for(longlongi=0;i<n;i++){tat[i]=ct[i]-c[i].arrTime;sum+=tat[i];}cout<<sum/n;return0;}
Hi. I also thought of and implemented something similar. it's passing sample test cases and cases 10,11 and 12.
Rest of the cases are failing.
could u pls help?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Average Waiting Time
You are viewing a single comment's thread. Return to all comments →
Hi. I also thought of and implemented something similar. it's passing sample test cases and cases 10,11 and 12. Rest of the cases are failing. could u pls help?