Codeforces 158B Taxi Solution & Explanation

Difficulty : 1100

Problem Description

After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, …, sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.

Output

Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.

Examples

Input5
1 2 4 3 3
Output4
Input8
2 3 4 4 2 1 3 1
Output5

Solution

C# Solution

In my initial attempt, I utilized an array and its indices to calculate the number of taxis.

However, this approach proved to be time-consuming.

Therefore, I recognized the necessity to find an alternative solution.

First try – Time limit exceeded 

int n = int.Parse(Console.ReadLine());  //number of groups 

int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int carCnt = 0;

for(int i=0; i<arr.Length;i++){
    int num = arr[i];   // number of children in the group
    
    if(num==4){
        carCnt+=1;
        arr[i] = 0;
    }
    else if(num==3){
        var idx = Array.IndexOf(arr,1);
        if(idx>-1){
            carCnt+=1;
            arr[i] = 0;
            arr[idx] = 0;
        }
    }
    else if(num==2){
        var idx = Array.LastIndexOf(arr,2);
        if(idx>-1 && idx != i){
            carCnt+=1;
            arr[i] = 0;
            arr[idx] = 0;
            continue;
        }
        idx = Array.IndexOf(arr,1);
        if(idx>-1){
            arr[i] = 0;
            arr[idx] = 3;
        }
    }
    else if(num==1) {
        var idx = Array.IndexOf(arr,3);
        if(idx>-1){
            carCnt+=1;
            arr[i] = 0;
            arr[idx] = 0;
            continue;
        }
        
        idx = Array.LastIndexOf(arr,2);
        if(idx>-1){
            arr[i] = 0;
            arr[idx] = 3;
            continue;
        }
        
        idx = Array.LastIndexOf(arr,1);
        if(idx>-1 && idx != i){
            arr[i] = 0;
            arr[idx] = 2;
            continue;
        }
    }
    
}

int nonZeroCount = arr.Count(x => x > 0);
Console.WriteLine(carCnt+nonZeroCount);

Solution1

int n = int.Parse(Console.ReadLine());  //number of groups 
 
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int carCnt = 0;
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;


foreach(var a in arr){
    if(a==1){
        cnt1+=1;
    }
    else if(a==2){
        cnt2+=1;
    }
    else if(a==3){
        cnt3+=1;
    }
    else{   //4
        carCnt+=1;
    }
}

while(cnt3>0 && cnt1>0){
    cnt3-=1;
    cnt1-=1;
    carCnt+=1;
}

while(cnt1>1){
    cnt1-=2;
    cnt2+=1;
}

while(cnt2>1){
    cnt2-=2;
    carCnt+=1;
}

// If there is exactly one cnt1 and one cnt2, they can be combined into one taxi
if(cnt1==1 && cnt2==1){
    Console.WriteLine(carCnt+1+cnt3);
}
else{
    Console.WriteLine(carCnt+cnt1+cnt2+cnt3);
}

The while loop while(cnt3 > 0 && cnt1 > 0) is there to pair groups of 1 child (cnt1) with groups of 3 children (cnt3) into taxis as long as there are both kinds available.

And in the next while loop, we combine two groups with 1 child each, treating them as a single group with 2 children for the subsequent evaluation.

In the following while loop(cnt2>1), we pair two groups with 2 children each, treating them as a single group with 4 children for counting taxis.

In the last if-else, we handle the final taxi count based on the remaining groups efficiently.


Conclusion

This is the second problem from codeforces contest – Dashboard – VK Cup 2012 Qualification Round 1 – Codeforces

You can find the solution for the first problem here – Codeforces 158A Next Round Solution & Explanation

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 158B – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *