当前位置:网站首页>codeforce 158B Taxi

codeforce 158B Taxi

2022-06-23 07:14:00 Empress Yu

Today, I forgot to write the difficult question , Did codeforce Temporary top one , In the future, it will still be the main force .

Problem - 158B - Codeforces

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

5
1 2 4 3 3

output

4

input

8
2 3 4 4 2 1 3 1

output

5

Note

In the first test we can sort the children into four cars like this:

  • the third group (consisting of four children),
  • the fourth group (consisting of three children),
  • the fifth group (consisting of three children),
  • the first and the second group (consisting of one and two children, correspondingly).

There are other ways to sort the groups into four cars.

That is to say , The number of people in a group is 1 To 4 people , Taxis can be jammed 4 personal . Different groups of people can gather together in a taxi , People in the same group cannot be separated , How many taxis do you need at least . 

Enter the first line : Total number of groups

Enter the second line : The number of people in each group

The result of doing the question

success

Method : greedy

Try to keep the number of taxis as small as possible , Then priority should be given to those with a large number of people to get on the bus first .

4 The direct group of individuals has no objection .

3 Individuals can communicate with 1 Those who make up a team make up a piece , You can't just 3 A group of people

2 Individuals can communicate with 2 Those who make up a team make up a piece , Otherwise, you can communicate with 1 If you join a team, you can join a team , Otherwise, only two people can come out alone

1 Individuals try to form 4 One set of , Get out and ride alone

import java.util.*;
 
public class Main {
    public static  void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int c = Integer.parseInt(sc.nextLine());
        int[] items = new int[5];
        for(int i = 0; i < c; i++){
            items[sc.nextInt()]++;
        }
 
        int ans = 0;
        ans += items[4];//4 People have formed a team 
 
        // Spell it 4 One of the 
        //3+1
        int team = Math.min(items[1],items[3]);
        ans += team;
        items[3]-=team;
        items[1]-=team;
        //3
        ans += items[3];
 
        // Handle 2 ers 
        // Spell it 4 personal  2+2 First spell 
        team = items[2]/2;
        ans += team;
        items[2]-=team*2;
 
        //2+1+1
        team = Math.min(items[1]/2,items[2]);
        ans += team;
        items[1]-=team*2;
        items[2]-=team;
 
        //2+1
        team = Math.min(items[1],items[2]);
        ans += team;
        items[2]-=team;
        items[1]-=team;
        //2
        ans += items[2];
 
        //1,4 One team is rounded up 
        if(items[1]>0){
            ans += (items[1]-1)/4+1;
        }
 
        System.out.println(ans);
    }
}

summary

Difficult in English ,codeforce Sometimes I write a lot of nonsense for fun , Then I can't understand many words .

原网站

版权声明
本文为[Empress Yu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230625279847.html