幼儿园小朋友站成一列,按位置1、2、 3... 顺序编号,每个小朋友都拿了若干糖果;请找出3位小朋友,他们拿着相同颜色的糖果,且他们拿的糖果总数不少于其他任何3位小朋友(拿相同颜色糖果)的糖果总数,如果存在多组这样的小朋友,则找出位置编号最小的小朋友所在的组。
设置的前提条件:
1)每个小朋友最少拿1颗糖,最多拿1024颗糖, 且只拿一种颜色的糖果;不存在两个小朋友拿相同颜色相同数目的糖果。
2)糖果颜色只有2种: 1为红色,2为蓝色。
输入描述:
第一行为小朋友的总人数N(N<=1024),后面的N行分别为第1到N小朋友拿到糖果的数量和颜色。
#package huawei92;
import java.util.*;
public class huawei1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = Integer.parseInt(sc.next());
arr[i][1] = Integer.parseInt(sc.next());
}
for (Object o : maxCount(arr)) {
System.out.println(o);
}
}
public static Object[] maxCount(int[][] arr) {
TreeMap<Integer, Integer> redMap = new TreeMap<Integer, Integer>();
TreeMap<Integer, Integer> blueMap = new TreeMap<Integer, Integer>();
//Num序号
int Num = 0;
int redCount = 0;
int blueCount = 0;
//key代表数量,value代表序号
for (int[] arr1 : arr) {
Num++;
if (arr1[1] == 1) {
redMap.put(arr1[0], Num);
redCount++;
} else {
blueMap.put(arr1[0], Num);
blueCount++;
}
}
int redSum = 0;
int blueSum = 0;
//存放序号
List<Integer> redList = new ArrayList<>(3);
List<Integer> blueList = new ArrayList<>(3);
Object[] redKey = redMap.keySet().toArray();
Object[] blueKey = blueMap.keySet().toArray();
if (redCount >= 3) {
for (int i = redKey.length - 1; i > redKey.length - 4; i--) {
redSum += (Integer) redKey[i];
redList.add(redMap.get((Integer) redKey[i]));
}
}
if (blueCount >= 3) {
for (int i = blueKey.length - 1; i > blueKey.length - 4; i--) {
blueSum += (Integer) blueKey[i];
blueList.add(blueMap.get((Integer) blueKey[i]));
}
}
Collections.reverse(redList);
Collections.reverse(blueList);
Object[] redxuhao = redList.toArray();
Object[] bluexuhao = blueList.toArray();
if (redCount < 3 && blueCount < 3) {
return null;
} else if (redCount >= 3 && blueCount < 3) {
for (int i = 0; i < redList.size(); i++) {
System.out.print(redxuhao[i] + " ");
}
System.out.println();
return new Object[]{1, redSum};
} else if (blueCount >= 3 && redCount < 3) {
for (int i = 0; i < blueList.size(); i++) {
System.out.print(bluexuhao[i] + " ");
}
System.out.println();
return new Object[]{2, blueSum};
} else if (blueSum < redSum) {
for (int i = 0; i < redList.size(); i++) {
System.out.print(redxuhao[i] + " ");
}
System.out.println();
return new Object[]{1, redSum};
}else if (blueSum > redSum) {
for (int i = 0; i < blueList.size(); i++) {
System.out.print(bluexuhao[i] + " ");
}
System.out.println();
return new Object[]{1, blueSum};
}
else {
int redhe = 0;
int bluehe = 0;
for (int i = 0; i < bluexuhao.length; i++) {
bluehe += (Integer) bluexuhao[i];
}
for (int i = 0; i < redxuhao.length; i++) {
redhe += (Integer) redxuhao[i];
}
if (bluehe > redhe) {
for (int i = 0; i < blueList.size(); i++) {
System.out.print(bluexuhao[i] + " ");
}
System.out.println();
return new Object[]{2, blueSum};
} else {
for (int i = 0; i < redList.size(); i++) {
System.out.print(redxuhao[i] + " ");
}
System.out.println();
System.out.println("1");
return new Object[]{1, redSum};
}
}
}
}