求助!PAT甲级1012,我用Java这样写,最后一个测试例会超时!

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");
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值