火车站附近的货物中转站负责将到站货物运往仓库,小明在中转站负责调度2K辆中转车( K 辆干货中转车, K 辆湿货中转车)。货物由不同供货商从各地发来,各地的货物是依次进站,然后小明按照卸货顺序依次装货到中转车上,一个供货商的货只能装到一辆车上,不能拆装,但是一辆车可以装多家供货商的货;中转车的限载货物量由小明统一制定,在完成货物中转的前提下,请问中转车的统一限载货物数最小值为多少。
输入描述:
第一行 length 表示供货商数量1<= length <=104
第二行 goods 表示供货数数组,1<= goods [ i ]<=104
第三行 types 表示对应货物类型, types [ i ]等于0或者1,0代表干货,1代表湿货第四行 k 表示单类中转车数量1<= k <= goods . length
输出描述:
运行结果输出一个整数,表示中转车统一限载货物数
示例1:输入输出示例仅供调试,后台判题数据一般不包含示例
输入
4
3 2 6 3
0 1 1 0
2
输出
6
说明
货物1和货物4为干货,由2两干货中转车中转、每辆车运输一个货物、限载为3货物2和货物3为湿货,由2两湿货中转车中转,每辆车运输一个货物,限载为6这样中转车统一限载货物数可以设置为6(千货车和湿货车限载最大值),是最小的取值.
示例2:输入输出示例仅供调试,后台判题数据一般不包含示例
输入
4
3 2 6 8
0 1 1 1
1
输出
16
说明
货物1为干货,由1两千货中转车中转,限载为3
货物2、货物3和货物4为湿货,由1两湿货中转车中转,限载为16
这样中转车统一限载货物数可以设置为16 (千货车和湿货车限载最大值),是最小的取值
备注:
1.中转车最多跑一趟仓库
Java 代码
import java.util.stream.Stream;
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;class Main {public static void main(String[] args) {// 处理输入Scanner in = new Scanner(System.in);int num = in.nextInt();Integer[] goods = new Integer[num];for (int i=0;i<num;i++) {goods[i] = in.nextInt();}ArrayList<Integer> wet_goods = new ArrayList<Integer>();ArrayList<Integer> dry_goods = new ArrayList<Integer>();for (int i=0;i<num;i++) {if (in.nextInt() == 0) dry_goods.add(goods[i]);elsewet_goods.add(goods[i]);}int k = in.nextInt();List<ArrayList<Integer>> datas = new ArrayList<ArrayList<Integer>>();datas.add(wet_goods);datas.add(dry_goods);int result = 0;for (ArrayList<Integer> data : datas){data.sort(Comparator.naturalOrder());int left = data.get(data.size()-1);int right = data.stream().reduce (Integer::sum).orElse (0);while (left < right) {int x = (left + right) / 2;int[] cap = new int[k];for (int i=0;i<k;i++) {cap[i] = x;}if (dfs(0, data.size(), k, cap, data,x))right = x;elseleft = x + 1;}result = Math.max(result, left);}System.out.println(result);}public static boolean dfs(int i, int n, int k, int[] cap, ArrayList<Integer> data,int x){if (i == n)return true;for (int j=0;j<k;j++){if (cap[j] >= data.get(i)){cap[j] -= data.get(i);if (dfs(i + 1, n, k, cap, data,x))return true;cap[j] += data.get(i);}if (cap[j] == x)break;}return false;}}
Python代码
import functools
import collections
import math
from itertools import combinations
from re import matchclass TreeNode:def __init__(self, val=0, left=None, right=None):self.val = valself.left = leftself.right = right#并查集模板
class UF:def __init__(self, n=0):self.count = nself.item = [0 for x in range(n+1)]for i in range(n):self.item[i] = idef find(self, x):if (x != self.item[x]):self.item[x] = self.find(self.item[x])return 0return xdef union_connect(self, x, y):x_item = self.find(x)y_item = self.find(y)if (x_item != y_item):self.item[y_item] = x_itemself.count-=1#输入
num = int(input())
datas = []
datas.append([int(x) for x in input().split(" ")])
datas.append([int(x) for x in input().split(" ")])
k = int(input())dry_goods = []
wet_goods = []
for i in range(len(datas[0])):if datas[1][i] == 0:dry_goods.append(datas[0][i])else:wet_goods.append(datas[0][i])def dfs(i):if i == n:return Truefor j in range(k):if cap[j] >= data[i]:cap[j] -= data[i]if dfs(i + 1):return Truecap[j] += data[i]if cap[j] == x:breakreturn Falser = 0
for data in [dry_goods, wet_goods]:n = len(data)data.sort(reverse=True)left, right = max(data), sum(data)while left < right:x = (left + right) // 2cap = [x] * kif dfs(0):right = xelse:left = x + 1r = max(r, left)
print(r)
JS代码
1