码迷,mamicode.com
首页 > 其他好文 > 详细

【Codeforces 158B】Taxi

时间:2019-01-28 16:40:44      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:int   .com   system   rest   col   while   i++   这一   port   

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


每辆车可以载重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);
    }
}

【Codeforces 158B】Taxi

标签:int   .com   system   rest   col   while   i++   这一   port   

原文地址:https://www.cnblogs.com/AWCXV/p/10330722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!