PAT甲级的1012题,排序题,我用Java这样写的,思路跟C++代码思路类似,但是最后一个测试用例会超时,还请大佬们看一看怎么修改可以通过~~
感谢!感谢!感谢!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
class Node {
int id, best;
int score[] = new int[4];
int rank[] = new int[4];
}
class NodeComparator implements Comparator<Node>
{
int flag = 0;
public NodeComparator(int flag)
{
this.flag = flag;
}
public int compare(Node o1, Node o2) {
return o2.score[flag] - o1.score[flag];
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] strs = line.split(" ");
int n = Integer.parseInt(strs[0]);
int m = Integer.parseInt(strs[1]);
Node stu[] = new Node[n];
for(int i=0; i<n; i++) {
stu[i] = new Node();
}
int exist[] = new int[1000000];
for(int i=0; i<n; ++i) {
strs = br.readLine().split(" ");
stu[i].id = Integer.parseInt(strs[0]);
stu[i].score[1] = Integer.parseInt(strs[1]);
stu[i].score[2] = Integer.parseInt(strs[2]);
stu[i].score[3] = Integer.parseInt(strs[3]);
stu[i].score[0] = (int) ((stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5);
}
for(int i=0; i<=3; i++) {
Arrays.sort(stu, new NodeComparator(i));
stu[0].rank[i] = 1;
for(int j=1; j<n; j++)
{
stu[j].rank[i] = j + 1;
if(stu[j].score[i]==stu[j-1].score[i])
stu[j].rank[i] = stu[j-1].rank[i];
}
}
for(int i=0; i<n; i++)
{
exist[stu[i].id] = i+1;
stu[i].best = 0;
int minn = stu[i].rank[0];
for(int j=1; j<=3; j++) {
if(stu[i].rank[j]<minn) {
minn = stu[i].rank[j];
stu[i].best = j;
}
}
}
char c[] = {'A', 'C', 'M', 'E'};
for(int i=0; i<m; i++) {
int temp = exist[Integer.parseInt(br.readLine())];
if(temp>0)
{
int best = stu[temp-1].best;
System.out.println(stu[temp-1].rank[best] + " " + c[best]);
}else {
System.out.println("N/A");
}
}
}
}