当前位置:网站首页>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 .
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 3output
4input
8 2 3 4 4 2 1 3 1output
5Note
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 .
边栏推荐
猜你喜欢
随机推荐
897. incremental sequential search tree
407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)
900. RLE 迭代器
306. Addenda
SSTable详解
深度学习系列47:超分模型Real-ESRGAN
异构交易场景交互流程及一致性保证
A small method of debugging equipment serial port information with ADB
901. stock price span
正则表达式图文超详细总结不用死记硬背(上篇)
关于#sql#的问题:有没有不增加字段,在原有字段的基础上,对字段里面的null值进行填充的方法呢
WPF command directive and inotifypropertychanged
Mysql事务隔离级别
【2022毕业季】从毕业到转入职场
Initialization layer implementation
npm下载报错npm ERR code ERESOLVE
EndNote20使用教程分享(未完
Pspnet complete code implementation
306. 累加数









