首页 > ACM题库 > 九度OJ > 九度-1005-Graduate Admission[解题代码]
2013
12-12

九度-1005-Graduate Admission[解题代码]

题目来源:2011年浙江大学计算机及软件工程研究生机试真题

题目描述:

    It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.
    Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE + GI) / 2. The admission rules are:

    • The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
    • If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
    • Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one’s turn to be admitted; and if the quota of one’s most preferred shcool is not exceeded, then one will be admitted to this school, or one’s other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
    • If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

输入:

    Each input file may contain more than one test case.
    Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.
    In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.
    Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant’s GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M-1, and the applicants are numbered from 0 to N-1.

输出:

    For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants’ numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

样例输入:
11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4
样例输出:
0 10
3
5 6 7
2 8

1 4

java 代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static Student students[] = null;
	static School schools[] = null;
	static int limits[] = null;
	
	public static void main(String[] args) {
		int strNum,schNum,choices;
		
		Scanner s = new Scanner(new BufferedInputStream(System.in));
		while(s.hasNextInt()){
			strNum = s.nextInt();
			schNum = s.nextInt();
			choices = s.nextInt();
			students = new Student[strNum];
			schools = new School[schNum];
			for(int i=0; i<schools.length; i++)
				schools[i] = new School();
			limits = new int[schNum];
			
			for(int i=0; i<schNum; i++)
				limits[i] = s.nextInt();

			for(int i=0; i<strNum; i++){
				students[i] = new Student();
				students[i].score1 = s.nextInt();
				students[i].score2 = s.nextInt();
				students[i].index = i;
				students[i].perfers = new int[choices];
				for(int j=0; j<choices; j++)
					students[i].perfers[j] = s.nextInt();
			}
			Arrays.sort(students);

			for(int i=0; i<students.length; i++){
				for(int j=0; j<choices; j++){
					ArrayList<Student> list = schools[students[i].perfers[j]].students;
					if(list.size() < limits[students[i].perfers[j]]){
						list.add(students[i]);
						//System.out.println("学校:" + students[i].perfers[j] + "学生:" + students[i].index +"  " +list.size());
						break;
					}else if(list.size() >= limits[students[i].perfers[j]]){
						if(students[i].compareTo(list.get(list.size()-1)) == 0 ){
							list.add(students[i]);
							break;
						}
							
					}
				}
			}
			for(int i=0; i<schools.length; i++){
				int arr[] = new int[schools[i].students.size()];
				
				for(int j=0; j<schools[i].students.size(); j++){
					arr[j] = schools[i].students.get(j).index;
				}
				Arrays.sort(arr);
				for(int j=0; j<arr.length; j++){
					if(j != arr.length-1)
						System.out.print( arr[j] + " ");
					else
						System.out.print(arr[j]);
				}
				System.out.println();
			}
			
		}
	}
}

class School{

	ArrayList<Student> students = new ArrayList<Student>();
}

class Student implements Comparable<Student>{
	public int index;
	int score1;
	int score2;
	int perfers[];
	
	public int compareTo(Student o) {
		double s1 = ((this.score1 + this.score2)/2.0);
		double s2 =  ((o.score1 + o.score2)/2.0);
		
		if(s1 == s2){
			return o.score1 -this.score1;
		}
		else if(s1 > s2){
			return -1;
		}else
			return 1;
			
		
	}
}

/**************************************************************
	Problem: 1005
	User: coder
	Language: Java
	Result: Accepted
	Time:120 ms
	Memory:18952 kb
****************************************************************/

cpp 代码如下:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;







public class Main {
	static Student students[] = null;
	static School schools[] = null;
	static int limits[] = null;
	
	public static void main(String[] args) {
		int strNum,schNum,choices;
		
		Scanner s = new Scanner(new BufferedInputStream(System.in));
		while(s.hasNextInt()){
			strNum = s.nextInt();
			schNum = s.nextInt();
			choices = s.nextInt();
			
			students = new Student[strNum];
			schools = new School[schNum];
			for(int i=0; i<schools.length; i++)
				schools[i] = new School();
			limits = new int[schNum];
			
			for(int i=0; i<schNum; i++)
				limits[i] = s.nextInt();

			for(int i=0; i<strNum; i++){
				students[i] = new Student();
				students[i].score1 = s.nextInt();
				students[i].score2 = s.nextInt();
				students[i].index = i;
				
				students[i].perfers = new int[choices];
				for(int j=0; j<choices; j++)
					students[i].perfers[j] = s.nextInt();
				
				
			}
			
			
			Arrays.sort(students);

			
			for(int i=0; i<students.length; i++){
				for(int j=0; j<choices; j++){
					ArrayList<Student> list = schools[students[i].perfers[j]].students;
					if(list.size() < limits[students[i].perfers[j]]){
						list.add(students[i]);
						//System.out.println("学校:" + students[i].perfers[j] + "学生:" + students[i].index +"  " +list.size());
						break;
					}else if(list.size() >= limits[students[i].perfers[j]]){
						if(students[i].compareTo(list.get(list.size()-1)) == 0 ){
							list.add(students[i]);
							//System.out.println("学校:" + students[i].perfers[j] + "学生:" + students[i].index +"  " +list.size());
							break;
						}
							
					}
				}
			}
			BufferedOutputStream out = new BufferedOutputStream(System.out);
			for(int i=0; i<schools.length; i++){
				int arr[] = new int[schools[i].students.size()];
				
				for(int j=0; j<schools[i].students.size(); j++){
					arr[j] = schools[i].students.get(j).index;
				}
				Arrays.sort(arr);
				for(int j=0; j<arr.length; j++){
					if(j != arr.length-1)
						System.out.print( arr[j] + " ");
					else
						System.out.print(arr[j]);
				}
				
				System.out.println();
				try {
					out.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}
	}
}

class School{

	ArrayList<Student> students = new ArrayList<Student>();
}

class Student implements Comparable<Student>{
	public int index;
	int score1;
	int score2;
	int perfers[];
	
	public int compareTo(Student o) {
		double s1 = ((this.score1 + this.score2)/2.0);
		double s2 =  ((o.score1 + o.score2)/2.0);
		
		if(s1 == s2){
			return o.score1 -this.score1;
		}
		else if(s1 > s2){
			return -1;
		}else
			return 1;
			
		
	}
}

/**************************************************************
	Problem: 1005
	User: coder
	Language: Java
	Result: Accepted
	Time:120 ms
	Memory:18992 kb
****************************************************************/