Header Ads Widget

Lumpy - The Bus Driver | codechef solution

Lumpy is a bus driver. Today, the conductor is absent so Lumpy has to do the conductor's job as well. There are N creatures in the bus. Sometimes the creatures don't carry change and can't pay the exact amount of the fare. Each creature in the bus today has paid an amount greater than his/her fare. You are given information about the extra amount paid by each creature, by an array A of size N, where Ai denotes the extra amount paid by the i-th creature, in rupees.

After the end of the trip, Lumpy noticed that he had P one rupee coins and Q two rupee coins. He wants to pay back the creatures using this money. Being a kind hearted moose, Lumpy wants to pay back as many creatures as he can. Note that Lumpy will not pay back the i-th creature if he can't pay the exact amount that the i-th creature requires with the coins that he possesses.

Lumpy is busy driving the bus and doesn't want to calculate the maximum number of creatures he can satisfy - He will surely cause an accident if he tries to do so. Can you help him out with this task?

Input

  • The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
  • For each test case, first line consists of three space separated integers NP and Q.
  • Second line consists of N space separated integers A containing N space integers, where i-th integer denotes Ai.

Output

  • For each test case, output a single line containing an integer corresponding to maximum number of creatures that Lumpy can pay back.

Constraints

  • 1 ≤ T ≤ 106
  • 1 ≤ N ≤ 105
  • 1 ≤ Ai ≤ 109
  • 0 ≤ P, Q ≤ 1014
  • Sum of N over all the cases does not exceed 106

Subtasks

  • Subtask #1 (15 points)P = 0
  • Subtask #2 (15 points)Q = 0
  • Subtask #3 (70 points): Original constraints

Sample 1:

Input
Output
3
3 3 0
1 2 2
3 2 1
1 2 1
4 5 4
2 3 4 5
2
3
3

Explanation:

Example 1. Lumpy has just 3 one rupee coins. He can pay creatures numbered {1, 2} or creatures numbered {1, 3} with these coins. Thus, answer is 2.

Example 2. Lumpy has 2 one rupee coins and 1 two rupee coin. In the optimal solution, Lumpy can give the two rupee coin to creature 2 and the one rupee coins to creatures 1 and 3. Thus, answer is 3. 

Code(C++):-

#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000007
#define ll long long
#define endl "\n"



void solve(){
    ll n,p,q;cin>>n>>p>>q;
    ll *a=new ll[n];
    for (int i = 0; i < n; ++i)
        {
            /* code */cin>>a[i];
        }   
    sort(a,a+n);ll count=0;
    for (int i = 0; p+2*q>=a[i]&&i<n; ++i)
    {   
        if(p==0&&a[i]&1) continue;
        
        int temp=a[i]/2;
        if(temp==0){}
        else if(temp<=q){q-=temp;a[i]=a[i]-temp*2;}
        else{a[i]-=2*q;q=0;}
        
        if(p>=a[i]){p-=a[i];a[i]=0;}
        if(a[i]==0)count++;

    }
    cout<<count<<endl;

}

int main(){
    

    int t;cin>>t;
    while(t--){
        solve();
    }
    
return 0;
}


Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine()),n;
        long p,q,c;
        while(t-->0)
        {
         String s[]=br.readLine().split(" ");
         n=Integer.parseInt(s[0]);
         p=Long.parseLong(s[1]);q=Long.parseLong(s[2]);c=0;
         s=br.readLine().split(" ");
         long a[]=new long[n];
         for(int i=0;i<n;i++)
         a[i]=Long.parseLong(s[i]);
         Arrays.sort(a);
         for(int i=0;i<n;i++)
         {
         if(p+2*q<a[i])
         break;
         if(a[i]%2==1)
         {
         if(p>0)
         {
         p--;
         a[i]--;
         }
         else
         continue;
         }
         if(a[i]==0)
         c++;
         if(a[i]>1)
         {
         if(2*q>=a[i])
         {
         q-=(a[i]/2);
         a[i]=0;
         c++;
         }
         else
         {
         a[i]-=2*q;
         q=0;
         p-=a[i];
         a[i]=0;
         c++;
         }
         }
         }
         System.out.println(c);
        }
    }
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-



Post a Comment

0 Comments