思路:我们可以知道,一定会有一个答案在所给的车数量中(包括0),因此我们只需要找到答案,并验证它。所以想到二分来查找答案,并验证是否可行。不过要注意的就是为了保证尽量多租,我们就优先用个人金额多的去租贵的车,以及验证的时候,要选便宜的。因此也要先排序两个数组b,p
代码:
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static Scanner sc = new Scanner(System.in);
static int n,m;
static long a;
static long ans;
static final int MAX = 1000001;
static int [] b;
static long [] p;
static boolean check(int x) {
long sum = 0;
for(int i = 0; i < x; i++) {
if(p[x-1-i] > b[n-1-i])
sum += p[x-1-i]-b[n-1-i];
}
if(sum > a)
return false;
return true;
}
public static void main(String[] args) throws IOException {
n = sc.nextInt(); m = sc.nextInt(); a = sc.nextLong();
b = new int[n];
p = new long[m];
for(int i = 0; i < n; i++) {
b[i] = sc.nextInt();
}
for(int i = 0; i < m; i++) {
p[i] = sc.nextLong();
}
Arrays.sort(b);
Arrays.sort(p);
int l = 0, r = Math.min(n, m), mid, ans = 0;//r要选n、m中小的,因为不能多于人数和车数
while(l<=r) {
mid = (l + r) >> 1;
if(check(mid)) {
l = mid + 1;
ans = mid;
}
else
r = mid - 1;
}
long sum = 0;
for(int i = 0; i < ans; i++)
sum += p[i];
sum = Math.max(0, sum - a);
System.out.println(ans+" "+sum);
System.exit(0);
}
}