【Codeforces 158B】Taxi

【链接】 我是链接,点我呀:)
【题意】

每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车

【题解】

将每个组的人数从小到大排序. 然后逆序枚举每个组r. 显然这个组肯定要占用一辆车。 那么现在问题就变成尽可能多带几个人在这一辆车里面。 那么就找人数最小的几个组就好(这样一辆车里面的人能多一点。)

【代码】

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	
	public static int N = 100000;
	
	public static int n,k;
	public static int s[];
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		
		n = in.nextInt();
		s = new int[n];
		
		for (int i = 0;i < n;i++) s[i] = in.nextInt();
		Arrays.sort(s);
		
		int l = 0,r = n-1,temp = 0;
		while (l<=r) {
			int rest = 4;
			rest-=s[r];r--;
			while (l<=r && rest>=s[l]) {
				rest-=s[l];
				l++;
			}
			temp++;
		}
		System.out.println(temp);
	}
}
原文地址:https://www.cnblogs.com/AWCXV/p/10330722.html