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.
inlineconstexprlongSTART=1,TOWN=2,END=3;structEvent{longposition{0};longtype{1};longpopulation{0};size_tcloud_idx{0};Event(longp,longt,longpopn,size_tidx=0):position(p),type(t),population{popn},cloud_idx{idx}{}booloperator<(constEvent&other){returnstd::tie(position,type)<std::tie(other.position,other.type);}};longmaximumPeople(vector<long>&p,vector<long>&x,vector<long>&y,vector<long>&r){// Return the maximum number of people that will be in a sunny town after removing exactly one cloud.std::vector<Event>all_events;std::transform(p.begin(),p.end(),x.begin(),std::back_inserter(all_events),[](longpopn,longpos){returnEvent(pos,TOWN,popn);});std::inner_product(y.begin(),y.end(),r.begin(),std::reference_wrapper(all_events),[idx=0ll](auto&&v,constauto&pair)mutable{constauto&[pos,rad]=pair;v.get().emplace_back(pos-rad,START,-1,idx);v.get().emplace_back(pos+rad,END,-1,idx);++idx;returnstd::move(v);},[](longpos,longrad){returnstd::make_pair(pos,rad);});// cout << all_events.size() << endl;std::sort(all_events.begin(),all_events.end());std::unordered_set<long>active_cloud;std::unordered_map<size_t,long>cloud_to_popn;longalways_sunny=0;for(constauto&event:all_events){switch(event.type){caseSTART:active_cloud.emplace(event.cloud_idx);break;caseTOWN:if(active_cloud.size()==1){cloud_to_popn[*active_cloud.begin()]+=event.population;}elseif(active_cloud.size()==0){always_sunny+=event.population;}break;caseEND:active_cloud.erase(event.cloud_idx);break;default:break;}}automax_elem=std::max_element(cloud_to_popn.begin(),cloud_to_popn.end(),[](constauto&p1,constauto&p2){returnp1.second<p2.second;});longret=always_sunny+(max_elem!=cloud_to_popn.end()?max_elem->second:0ll);returnret;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cloudy Day
You are viewing a single comment's thread. Return to all comments →