题目描述
对以下三种情况取最大值
s = x - y - z = w1 +…+ wn > 0 (w1 = A1 - B1 - C2 ....)
s = y - x - z = w1 +…+ wn > 0 (w1 = B1 - A1 - C2 ....)
s = z - x - y = w1 +…+ wn > 0 (w1 = C2 - A1 - B1 ....)
对于第一种情况,先把w1至wn 放入一个数组a中,然后从小到大排序
从length-1到0枚举数组a, sum += a[i] 如果sum[i] > 0 res = a.length - i;
贪心
import java.util.*;
public class Main {
static int N = 100010;
static int[] a = new int[N];
static int[] b = new int[N];
static int[] c = new int[N];
static int work(int[] x, int[] y, int[] z) {
int[] w = new int[x.length];
for (int i = 0; i < x.length; i++) {
w[i] = x[i] - y[i] - z[i];
}
//从小到大排序
Arrays.sort(w);
int res = -1;
long sum = 0;
//从大往小枚举
for (int i = x.length - 1; i >= 0; i--) {
sum += w[i];
if (sum > 0) {
res = x.length - i;
} else {
break;
}
}
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
b[i] = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
c[i] = scanner.nextInt();
}
int res = Math.max(Math.max(work(a, b, c), work(b, a, c)), work(c, a, b));
System.out.println(res);
}
}