1012 The Best Rank (排序or统计)

1012 The Best Rank (25)(25 分)

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

For example, The grades of C, M, E and A - Average of 4 students aregiven 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 onehas done the best in C Programming Language, while the 2nd one inMathematics, the 3rd one in English, and the last one in average.

Input

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

Output

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 thesame 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


Analysis:

At first glance,this problem seems to be  a sort question. After sort, you can get a monotonous decreasing  array, then calculate their ranks.(P.S: if the top three scores are 90,90,85,the ranks relatively  are  1,1,3.)The time complexity arrive at nlog(n).

if you use scanner to input data ,it will be time limit exceeded.

Here is a better input method:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Score{
	public int id;
	public int score;
	public Score(int id,int score) {
		super();
		this.id=id;
		this.score = score;
	}
}
public class Main {
	static short id[][]=new short[1000000][4];
	public static Comparator<Score> cmp=new Comparator<Score>() {
		public int compare(Score o1, Score o2) {
			return o2.score-o1.score;
		};
	};
	public static void print(int t){		
		int min=id[t][0];
		char c='A';
		char ch[]= {'A','C','M','E'};
		for (int i=1;i<4;i++) {
			if(id[t][i]<min) {
				c=ch[i];
				min=id[t][i];
			}
		}
		System.out.printf("%d %c\n",min,c);
	}
	public static void main(String[] args) throws IOException {	//bufferedreader must throw exception
		int n,m,M,E,C,num;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String mn[]=br.readLine().split(" ");//split the String and convert to int
		n=Integer.parseInt(mn[0]);
		m=Integer.parseInt(mn[1]);
		ArrayList<Score>ala=new ArrayList<Score>();
		ArrayList<Score>alc=new ArrayList<Score>();
		ArrayList<Score>alm=new ArrayList<Score>();
		ArrayList<Score>ale=new ArrayList<Score>();
		for(int i=0;i<n;i++) {
			mn=br.readLine().split(" ");
			num=Integer.parseInt(mn[0]);
			C=Integer.parseInt(mn[1]);			
			M=Integer.parseInt(mn[2]);
			E=Integer.parseInt(mn[3]);
			ala.add(new Score(num,(int)((C+M+E)/3.0+0.5)));//four arrays sort respectively
			alc.add(new Score(num,C));
			alm.add(new Score(num,M));
			ale.add(new Score(num,E));
		}
		Collections.sort(ala, cmp);//sort
		id[ala.get(0).id][0]=1;
		for(int i=1;i<ala.size();i++)//calculate the rank
			if(ala.get(i).score==ala.get(i-1).score) 
				id[ala.get(i).id][0]=id[ala.get(i-1).id][0];
			else
				id[ala.get(i).id][0]=(short) (i+1);
		Collections.sort(alc, cmp);
		id[alc.get(0).id][1]=1;
		for(int i=1;i<alc.size();i++)
			if(alc.get(i).score==alc.get(i-1).score) 
				id[alc.get(i).id][1]=id[alc.get(i-1).id][1];		
			else
				id[alc.get(i).id][1]=(short) (1+i);
		Collections.sort(ale,cmp);
		id[ale.get(0).id][3]=1;
		for(int i=1;i<ale.size();i++)
			if(ale.get(i).score==ale.get(i-1).score) 
				id[ale.get(i).id][3]=id[ale.get(i-1).id][3];
			else
				id[ale.get(i).id][3]=(short) (1+i);
		Collections.sort(alm, cmp);		
		id[alm.get(0).id][2]=1;
		for(int i=1;i<alm.size();i++)
			if(alm.get(i).score==alm.get(i-1).score) 
				id[alm.get(i).id][2]=id[alm.get(i-1).id][2];
			else
				id[alm.get(i).id][2]=(short) (1+i);
		for(int i=0;i<m;i++) {
			int t=Integer.parseInt(br.readLine());
			if(id[t][0]==0)//not exist(default value :0)
				System.out.println("N/A");
			else
				print(t);	
		}
		br.close();
	}

}

After careful consideration, a better algorithm is to count instead of sort ,use a array to store information of  the rank of every score.Then input the id of every student ,query the rank of his grade,select the minimun .

package A1012;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
class Score{
	public int c;
	public int m;
	public int a;
	public int e;
	public Score(int c, int m, int e,int a) {
		super();
		this.c = c;
		this.m = m;
		this.e = e;
		this.a=a;
	}
}
public class Main {
	static int score[][]=new int [101][4];
	/**
	 * score[i][j] stands for the num of student who get i points in subject j.
	 * @param s
	 */
	public static void min(Score s){		
		int min=score[s.a][0];
		char c='A';
		char ch[]= {'C','M','E'};
		int rank[]= {score[s.c][1],score[s.m][2],score[s.e][3]};
		for (int i=0;i<rank.length;i++) {
			if(rank[i]<min) {
				c=ch[i];
				min=rank[i];
			}
		}
		System.out.printf("%d %c\n",min,c);
	}
	public static void main(String[] args) throws IOException {	
		int n,m,M,E,C,A,num;		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String mn[]=br.readLine().split(" ");
		n=Integer.parseInt(mn[0]);
		m=Integer.parseInt(mn[1]);
		HashMap<Integer,Score>map=new HashMap<Integer,Score>();
		for(int i=0;i<n;i++) {
			mn=br.readLine().split(" ");
			num=Integer.parseInt(mn[0]);
			C=Integer.parseInt(mn[1]);score[C][1]++;			
			M=Integer.parseInt(mn[2]);score[M][2]++;
			E=Integer.parseInt(mn[3]);score[E][3]++;
			A=(int)((C+M+E)/3.0+0.5);score[A][0]++;
			map.put(num,new Score(C,M,E,A));
		}
		for(int i=0;i<4;i++) {
			int count=0;
			for(int j=100;j>=0;j--) {
				if(score[j][i]!=0) {//suffix sum
					int temp=score[j][i];
					score[j][i]=count+1;
					count+=temp;					
				}
			}
		}
		/**
		 * now score[i][j] stands for the rank of a student who gets i points in subject j;
		 */
		for(int i=0;i<m;i++) {
			int t=Integer.parseInt(br.readLine());
			if(map.get(t)==null)
				System.out.println("N/A");
			else
				min(map.get(t));	
		}
		br.close();
	}
}






猜你喜欢

转载自blog.csdn.net/zpjlkjxy/article/details/80649198