1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class A1012 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
ArrayList<CSStudent> arr = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] line = br.readLine().split(" ");
String id = line[0];
int C = Integer.parseInt(line[1]);
int M = Integer.parseInt(line[2]);
int E = Integer.parseInt(line[3]);
arr.add(new CSStudent(id, C, M, E, (int) Math.round((C + M + E) / 3.0)));
}
ArrayList<CSStudent> aRank = new ArrayList<>(arr);
ArrayList<CSStudent> cRank = new ArrayList<>(arr);
ArrayList<CSStudent> mRank = new ArrayList<>(arr);
ArrayList<CSStudent> eRank = new ArrayList<>(arr);
Collections.sort(aRank, new Comparator<CSStudent>() {
@Override
public int compare(CSStudent o1, CSStudent o2) {
return o2.getA() - o1.getA();
}
});
Collections.sort(cRank, new Comparator<CSStudent>() {
@Override
public int compare(CSStudent o1, CSStudent o2) {
return o2.getC() - o1.getC();
}
});
Collections.sort(mRank, new Comparator<CSStudent>() {
@Override
public int compare(CSStudent o1, CSStudent o2) {
return o2.getM() - o1.getM();
}
});
Collections.sort(eRank, new Comparator<CSStudent>() {
@Override
public int compare(CSStudent o1, CSStudent o2) {
return o2.getE() - o1.getE();
}
});
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < aRank.size(); j++) {
if (arr.get(i).getId().equals(aRank.get(j).getId())) {
if (j != 0 && aRank.get(j).getA() == aRank.get(j - 1).getA()) {
aRank.get(j).setaLevel(j);
} else {
aRank.get(j).setaLevel(j + 1);
}
break;
}
}
for (int j = 0; j < cRank.size(); j++) {
if (arr.get(i).getId().equals(cRank.get(j).getId())) {
if (j != 0 && cRank.get(j).getC() == cRank.get(j - 1).getC())
cRank.get(j).setcLevel(j);
else
cRank.get(j).setcLevel(j + 1);
break;
}
}
for (int j = 0; j < mRank.size(); j++) {
if (arr.get(i).getId().equals(mRank.get(j).getId())) {
if (j != 0 && mRank.get(j).getM() == mRank.get(j - 1).getM())
mRank.get(j).setmLevel(j);
else
mRank.get(j).setmLevel(j + 1);
break;
}
}
for (int j = 0; j < eRank.size(); j++) {
if (arr.get(i).getId().equals(eRank.get(j).getId())) {
if (j != 0 && eRank.get(j).getE() == eRank.get(j - 1).getE())
eRank.get(j).seteLevel(j);
else
eRank.get(j).seteLevel(j + 1);
break;
}
}
}
char[] ch = {'A', 'C', 'M', 'E'};
for (int i = 0; i < m; i++) {
String id = br.readLine();
boolean flag = false;
int minLevel = arr.size() + 1;
int n = 0;
for (int j = 0; j < arr.size(); j++) {
if (arr.get(j).getId().equals(id)) {
flag = true;
int a = arr.get(j).getaLevel();
int c = arr.get(j).getcLevel();
int mm = arr.get(j).getmLevel();
int e = arr.get(j).geteLevel();
int[] level = new int[]{a, c, mm, e};
for (int k = 0; k < 4; k++) {
if (level[k] < minLevel) {
minLevel = level[k];
n = k;
}
}
break;
}
}
if (flag) {
System.out.println(minLevel + " " + ch[n]);
} else {
System.out.println("N/A");
}
}
}
}
class CSStudent {
private String id;
private int C;
private int M;
private int E;
private int A;
private int aLevel;
private int cLevel;
private int mLevel;
private int eLevel;
public CSStudent(String id, int c, int m, int e, int a) {
this.id = id;
C = c;
M = m;
E = e;
A = a;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getC() {
return C;
}
public void setC(int c) {
C = c;
}
public int getM() {
return M;
}
public void setM(int m) {
M = m;
}
public int getE() {
return E;
}
public void setE(int e) {
E = e;
}
public int getA() {
return A;
}
public void setA(int a) {
A = a;
}
public int getaLevel() {
return aLevel;
}
public void setaLevel(int aLevel) {
this.aLevel = aLevel;
}
public int getcLevel() {
return cLevel;
}
public void setcLevel(int cLevel) {
this.cLevel = cLevel;
}
public int getmLevel() {
return mLevel;
}
public void setmLevel(int mLevel) {
this.mLevel = mLevel;
}
public int geteLevel() {
return eLevel;
}
public void seteLevel(int eLevel) {
this.eLevel = eLevel;
}
}
3 要点
(1)本题意思简单,但是代码量较多,用Java会导致超时(我的菜鸡代码会,好像有大佬用Java写的没有超时)。建议C++。
(2)思路:读入所有学生的信息,然后按要求分别排序,最后按要求输出。注意同一分数的学生排名相同,即允许并列。关键代码:
if (arr.get(i).getId().equals(aRank.get(j).getId())) {
if (j != 0 && aRank.get(j).getA() == aRank.get(j - 1).getA())
aRank.get(j).setaLevel(j);
} else {
aRank.get(j).setaLevel(j + 1);
}