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.
//Define the structs Workshops and Available_Workshops.//Implement the functions initialize and CalculateMaxWorkshops#include<sstream>structWorkshop;booloperator<(constWorkshop&lhs,constWorkshop&rhs);structWorkshop{Workshop(intstart,intdura):m_start(start),m_duration(dura),m_end(start+dura){}Workshop(constWorkshop&other):m_start(other.m_start),m_duration(other.m_duration),m_end(other.m_start+other.m_duration){}stringdump(void)const{ostringstreamoss;oss<<"["<<m_start<<", "<<m_end<<"]";returnoss.str();}friendbooloperator<(constWorkshop&lhs,constWorkshop&rhs);intm_start;intm_duration;intm_end;};booloperator<(constWorkshop&lhs,constWorkshop&rhs){if(lhs.m_end==rhs.m_end){returnlhs.m_start<rhs.m_start;}else{returnlhs.m_end<rhs.m_end;}}structAvailable_Workshops{Available_Workshops(void){}Available_Workshops&add_workshop(constWorkshop&ws){autob=m_available.begin();autoe=m_available.end();autopos=lower_bound(b,e,ws);m_available.insert(pos,ws);return*this;}intmax_nonoverlap(void)const;vector<Workshop>m_available;};intAvailable_Workshops::max_nonoverlap(void)const{if(m_available.empty()){return0;}// debugcerr<<"all workshops: "<<endl;for(auto&ws:m_available){cerr<<'\t'<<ws.dump()<<endl;}intstart{0};intend{0};intcount{0};autoit=m_available.begin();cerr<<"nonoverlap workshops: "<<endl;for(;it!=m_available.end();++it){if(it->m_end<=end){continue;}if(it->m_start<end){continue;}cerr<<"\t"<<it->dump()<<std::endl;count+=1;start=it->m_start;end=it->m_end;}returncount;}intCalculateMaxWorkshops(constAvailable_Workshops*aw){returnaw->max_nonoverlap();}Available_Workshops*initialize(int*starts,int*durs,intn){Available_Workshops*available=newAvailable_Workshops();for(inti=0;i<n;++i){available->add_workshop(Workshop(starts[i],durs[i]));}returnavailable;// todo}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Attending Workshops
You are viewing a single comment's thread. Return to all comments →