在线评测链接
题目描述
有m个集装箱,n个叉车,每个叉车只能运一个集装箱。叉车的载重量大于等于集装箱重量就可以运送该集装箱。一共有x个增强组件,每个组件都可以使叉车的举重量增加y,但是每个叉车只能使用至多一个组件,且一个组件只能安装到至多一个叉车上。问最多可以运送多少个集装箱。
输入描述
第一行四个整数 m , n , x , y m,n,x,y m,n,x,y。
第二行m个整数,表示m个货物的重量
第三行n个整数,表示n个叉车的载重量
输出描述
输出最多可以运送多少个集装箱
样例
输入
5 5 2 5
9 5 7 8 5
1 6 2 6 4
输出
4
思路:二分答案 + 贪心算法
原题链接:2071. 你可以安排的最多任务数目 - 力扣(LeetCode)
这个问题可以通过二分答案和贪心算法来解决。我们的目标是找到最大的集装箱数量
首先,我们对叉车和集装箱按照重量进行排序。这样我们可以保证每次都是最轻的叉车去运送最轻的集装箱。
然后,我们使用二分答案来找到最大的满足条件的集装箱数量。我们设定一个范围,最小值为-1(表示没有一个集装箱可以被运送),最大值为m和n的最小值加1(表示所有的集装箱都可以被运送)。
在二分查找的每一步,我们都会尝试一个中间的集装箱数量,然后检查是否所有的叉车都能在使用增强组件的情况下运送这么多的集装箱。我们使用一个check
函数来进行这个检查。
check
函数会遍历所有的叉车,对于每一个叉车,如果它的载重量大于等于当前的集装箱的重量,那么它就可以运送这个集装箱,我们就将这个集装箱从列表中移除。否则,我们就需要使用一个增强组件来增加叉车的载重量。我们使用二分搜索来找到一个可以使叉车的载重量大于等于当前集装箱重量的增强组件。如果我们找到了这样的增强组件,我们就将它从列表中移除,然后继续下一个叉车。如果我们没有找到这样的增强组件,或者增强组件已经用完了,那么我们就返回False
,表示当前的集装箱数量不满足条件。
-
如果
check
函数返回True
,表示当前的集装箱数量满足条件,我们就尝试增大集装箱数量。否则,我们就尝试减小集装箱数量。 -
当我们找到了最大的满足条件的集装箱数量,我们就输出这个数量。
时间复杂度
O ( n l o g m ) O(nlogm) O(nlogm)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int m, n, x, y;
int weight[50005];
int load[50005];
bool is_get[50005];
bool check(int m, int n, int cur_res) {
int cur_x = 0;
vector<int> weight_copy(weight, weight + cur_res);
vector<int> load_copy(load+n-cur_res, load + n);
for (int i = weight_copy.size() - 1; i >= 0; --i) {
bool is_find = false;
for (int j = 0; j < load_copy.size(); ++j) {
if (weight_copy[i] <= load_copy[j]) {
load_copy.erase(load_copy.begin() + j);
is_find = true;
break;
}
}
if (!is_find) {
for (int j = 0; j < load_copy.size(); ++j) {
if (weight_copy[i] <= load_copy[j] + y && cur_x < x) {
load_copy.erase(load_copy.begin() + j);
cur_x++;
is_find = true;
break;
}
}
}
if (!is_find)
return false;
}
return true;
}
int main()
{
int res = 0;
cin >> m >> n >> x >> y;
for (int i = 0; i < m; ++i) {
cin >> weight[i];
}
for (int i = 0; i < n; ++i) {
cin >> load[i];
}
sort(weight, weight + m);
sort(load, load + n);
int left = 1, right = m;
while (left <= right) {
int mid = left + (right - left) / 2;
if (check(m, n, mid)) {
res = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
cout << res << endl;
return 0;
}
python代码
# from sortedcontainers import SortedList
import bisect
def check(ts, ws):
p, s = pills, strength
for i in range(len(ws)):
if ws[i] >= ts[0]:
ts.pop(0)
else:
idx = bisect.bisect_right(ts, ws[i]+s)
if idx == 0 or p==0:
return False
p -= 1
ts.pop(idx-1)
return True
m, n, pills, strength = list(map(int, input().split()))
tasks = list(map(int, input().split()))
workers = list(map(int, input().split()))
tasks.sort()
workers.sort()
lo, hi = -1, min(m, n)+1
while lo+1<hi:
mid = (lo+hi)//2
if check(tasks[:mid], workers[n-mid:n]):
lo = mid
else:
hi = mid
print(lo)
Java代码
import java.util.*;
public class Main {
static int m;//货物的数量
static int n;//卡车的数量
static int x;//拖斗的数量 x=num
static int y;//每个拖斗的载重量
static int[] weights;//每个货物的重量 weights=tasks
static int[] loads;//每个卡车的载重量 loads=can
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
m = sc.nextInt();//货物数量
n = sc.nextInt();//卡车数量
x = sc.nextInt();//拖斗数量
y = sc.nextInt();//拖斗载重
weights = new int[m];//存储每个货物重量
for (int i = 0; i < m; i++) weights[i] = sc.nextInt();
loads = new int[n];//存储每个卡车的载重
for (int i = 0; i < n; i++) loads[i] = sc.nextInt();
sc.close();
//货物按重量升序排序
Arrays.sort(weights);
//卡车按载重量升序排序
Arrays.sort(loads);
//贪心+二分
int left = 0, right = m;
while(left < right) {
int mid = (left+right+1)>>1;
if(check(mid)){
left=mid;
}else{
right=mid-1;
}
}
System.out.println(left);
}
public static boolean check(int mid) {
if (mid > n) return false;//货物数量 > 卡车数量
//TreeMap可自动排序(默认按key升序排序),key:卡车载重量 value:该载重量卡车的数目
//map中按载重量从低到高存储卡车
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = n - mid; i < n; i++)
map.compute(loads[i], (k, v) -> (v == null ? 1 : v + 1));
//货物从重量大的 到 重量小的枚举
//贪心思路:尽量用载重量小的卡车运输 重量重的货物,这样每个卡车的盈余小一点
int t = x;
for (int i = mid - 1; i >= 0; i--) {
int weight = weights[i];//当前枚举的货物重量
Map.Entry<Integer, Integer> en = map.lastEntry();//map中载重量最大的卡车
if (en.getKey() >= weight) {
map.compute(en.getKey(), (k, v) -> (v <= 1 ? null : v - 1));
} else if (t > 0 && (en = map.ceilingEntry(weight - y)) != null) {
t--;
map.compute(en.getKey(), (k, v) -> (v <= 1 ? null : v - 1));
} else {
return false;
}
}
return true;
}
}