1012 The Best Rank (排序)

1012 The Best Rank (25 分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

 思路:

1、本质上是个排序问题,这里直接用了java的快排库函数Arrays.sort( ).

2、为了查找的时候方便,这里使用了哈希表结构,对于每科成绩,以分数为key,以排名为value。先把每科成绩从高到低排列之后。插入哈希表中。以后查找的时候可以直接根据分数拿名次。否则时间复杂度就是N乘以你拿名次的时间复杂度。

3、存储学号也用哈希表存,学号是key,数组下标为value,这样查找的时候直接根据学号拿下标,然后直接拿到成绩,再直接拿到排名,然后输出,这个算法的时间复度就取决于快排的时间复杂度,在一般情况下快排可以到NlogN,毕竟一场考试最重要的是时间,自己造轮子恐怕太麻烦。

4、结果吧,提交5次大概还是有1次超时,说明快排这个排序还是不稳定。还是C语言好,暴力搜索都不超时。

5、一开始顺序看错了 A C M E,搞成了A C E M,老有两个测试点过不去,尴尬。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {

	static int num[];
	static int C[];
	static int E[];
	static int M[];
	static int A[];
	static int n,m;
	static int a[];
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		StreamTokenizer in = new StreamTokenizer(
				new BufferedReader(new InputStreamReader(System.in)));
		in.nextToken();
		n = (int) in.nval;
		in.nextToken();
		m = (int) in.nval;
		
		num = new int[n];
		C = new int[n];
		E = new int[n];
		M = new int[n];
		A = new int[n];
		a = new int[n];
		
		int[] c = new int[n];
		int[] e = new int[n];
		int[] ma = new int[n];
		int[] av = new int[n];
		
		
		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
		
		
		for(int i = 0;i<n;i++) {
			in.nextToken();
			num[i] = (int) in.nval;
			map.put(num[i], i);
			
			in.nextToken();
			C[i] = (int) in.nval;
			c[i] = C[i];
			in.nextToken();
			M[i] = (int) in.nval;
			ma[i] = M[i];
			in.nextToken();
			E[i] = (int) in.nval;
			e[i] = E[i];
			a[i] = Math.round((float)(c[i] + e[i]+ma[i])/3);
			av[i] = a[i];
		}
		

		Map<Integer,Integer> c1 = retmap(c);
		Map<Integer,Integer> m1 = retmap(ma);
		Map<Integer,Integer> e1 = retmap(e);
		Map<Integer,Integer> a1 = retmap(av);
		
		for(int i=0;i<m;i++) {
			in.nextToken();
			int stu = (int) in.nval;
			sop(stu,c1,m1,e1,a1,map);
		}
		
	
	}
	private static int sop(int stu, Map<Integer, Integer> c1, Map<Integer, Integer> m1, Map<Integer, Integer> e1, Map<Integer, Integer> a1, Map<Integer, Integer> map) {
		// TODO Auto-generated method stub
		int index =-1;
		if(map.containsKey(stu)) {
			index = map.get(stu);
		}
		
		if(index ==-1) {
			System.out.println("N/A");
			return 0;
		}
		
		int rank[] = new int[4];
		rank[0] = a1.get(a[index]);
		rank[1] = c1.get(C[index]);
		rank[2] = m1.get(M[index]);
		rank[3] = e1.get(E[index]);
		
		int count = -1;
		int r =Integer.MAX_VALUE;
		for(int i=0;i<4;i++) {
			if(rank[i]<r) {
				r = rank[i];
				count = i;
			}
		}
		
		String str = inttostr(count);
		System.out.println(r+" "+str);
		
		return 0;
		
	}
	private static String inttostr(int count) {
		// TODO Auto-generated method stub
		switch(count) {
		case 0:return "A";
		case 1:return "C";
		case 2:return "M";
		case 3:return "E";
		}
		return null;
		
	}
	private static Map<Integer, Integer> retmap(int[] c) {
		// TODO Auto-generated method stub
		Arrays.sort(c);
		c = reverse(c);
		Map<Integer,Integer> map = new HashMap<>();
		
		int score = -1;
		int rank =0;
		int count=1;
		int index =0;
		
		while(index<n) {
			if(c[index] == score) {
				index++;
				count++;
			}
			else {
				rank = rank+count;
				map.put(c[index], rank);
				score = c[index];
				index++;
				count=1;
			}
		}
		
		return map;
	}
	private static int []  reverse(int[] c) {
		// TODO Auto-generated method stub
		int temp[] = new int[n];
		for(int i=0;i<n;i++) {
			temp[i] = c[n-1-i]; 
		}
		return temp;
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值