题目描述:
小蓝正在玩一款游戏。游戏中魏蜀吴三个国家各自拥有一定数量的士兵X, Y, Z (一开始可以认为都为 0 )。游戏有 n 个可能会发生的事件,每个事件之间相互独立且最多只会发生一次,当第 i 个事件发生时会分别让 X, Y, Z 增加Ai , Bi ,Ci 。
当游戏结束时 (所有事件的发生与否已经确定),如果 X, Y, Z 的其中一个大于另外两个之和,我们认为其获胜。例如,当 X > Y + Z 时,我们认为魏国获胜。小蓝想知道游戏结束时如果有其中一个国家获胜,最多发生了多少个事件?
如果不存在任何能让某国获胜的情况,请输出 −1 。
代码:
package lanqiao;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.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] = sc.nextInt();
}
for(int i = 0;i < n;i ++)
{
b[i] = sc.nextInt();
}
for(int i = 0;i < n;i ++)
{
c[i] = sc.nextInt();
}
for(int i = 0;i <n;i ++)
{
int m = a[i],e = b[i],f = c[i];
a[i] = m - e - f;
b[i] = e - m - f;
c[i] = f - m - e;
}
Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);
int d = -1;
long sum = 0,sun = 0,sus = 0;
for(int i = n - 1;i >= 0;i --)
{
sum += a[i];
sun += b[i];
sus += c[i];
if(sum > 0 || sun > 0 || sus > 0){
d = n - i;
}
}
System.out.println(d);
}
}