# Last Passenger in the Bus ## Problem Statement *There are a number of passengers waiting in a queue. Initially, the bus is assumed to be empty and has a fixed capacity "a". We need to find the last person who is entering a bus based on his patience level in accordance with the bus time. The person will be waiting for the bus until his patience level expires. In the end, if all the passengers are boarded, 0 is returned because there will be no person left.* ## Test Cases ### Test Case 1 ``` Input: Bus capacity a=2 List of Patience limits b=[1,2,3,4] List of Time of bus arrival c=[1,3,4] Output: [2,4,0] ``` Exaplation: * In this scenario, when the time of bus arrival(c[0]) is 1, all the passengers will be available( coz the patience level of all passengers is above bus time i.e 1). But as the bus capacity is limited to 2, only 2 passengers are allowed to enter. So only 2 passengers up to position 2 of the queue will be able to enter. So, the answer to the first query of bus arrival is 2. * * When the time of bus arrival(c[1]) is 3, passengers-1 & 2 will not be available as their patience level is exceeded, hence passengers at positions 3 and 4 will be entering according to the capacity. So, the answer to the second query of bus arrival is 4. * * When the time of bus arrival(c[2]) is 4, passengers 1,2& 3 would have left queue already, hence passenger 4 will be ready to enter, but as the capacity is not met, 0 is returned. So, final solution is [2,4,0]. ## Solution Approach * Persons are waiting in a queue so it'll be Fifo operation. * Bus arrival time aaray can be sorted, as the bus arriving early is more likely to take the least patient person, provided he's not so far in the queue. * we'll iterate through the bus array, and for evey bus we'll take all the perrsenger that are currently in the queue(skipping thode who have left or have already entered into some other bus) * Once the bus reaches the capaciy or the's no person remaining, we'll push the (index+1) of the last person entered into the array, if non entered we'll push 0. ## Psuedo code * Take tree vaiables: * i, iterator over bus array * j, iterator pessenger's array * k, person last entered in ith bus * initially, i=0 and j=0 * k=0 for every i (initially) * Now, for every i, from the current j, try to update k, constrained by capacity of the bus and number of passengers. * push k into answer array and check for next i, make sure to put k=0, for every i. ## Code ```c++-17 #include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define fr(i,a,b) for(ll i=a;i<b;i++) #define rf(i,a,b) for(ll i=a;i>=b;i--) #define endl "\n" #define pri(x) cout << x << "\n" #define prin(x,y) cout << x << " " << y << "\n" #define pb push_bacm #define yes() cout<<"YES"<<"\n" #define no() cout<<"NO"<<"\n" #define errA(A) for(auto i:A) cout<<i<<" ";cout<<"\n"; #define F first #define S second #define all(x) x.begin(), x.end() ll mod = 1e9+7; ll N=2e6; using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void outwer(){ ll n=0,m=0; cin>>n>>m; ll capacity=0; cin>>capacity; vector<ll>patience(m); vector<ll>arrival(n); fr(i,0,m){ cin>>patience[i]; } fr(i,0,n){ cin>>arrival[i]; } vector<ll>answer(n,0); ll i=0,j=0; for(;i<n;i++){ ll lastPerson=0; ll count =0; bool broken=false; for(;j<m;j++){ if(patience[j]>=arrival[i]){ lastPerson=j+1; count++; } if(count==capacity) { broken=true; break; } } if(broken) j++; answer[i]=lastPerson; } for(ll a:answer){ cout<<a<<" "; } cout<<endl; } int main(){ ios::sync_with_stdio(0),cin.tie(0); ll t=0; cin>>t; while(t--){ outwer(); } } ```