经典指数          
原因
963
浏览数
0
收藏数
 

It is said that in 2013, there were 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 contains 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

     举报   纠错  
 
切换
1 个答案
给个华丽丽的C#的玩法吧: “盲目”地使用Linq就可以非常轻松愉快地完成从主体数据输入到完成排序的任务,示例如下(nextInt是一个自己实现的函数,经试验,就算拿着记事本也是容易实现的) var kList = E.Range(0, k); var studentList = ( from i in E.Range(0, n) let GE = nextInt() let GT = GE + nextInt() let APP = ( from j in kList select nextInt() ).ToArray() orderby GT descending, GE descending select new { id = i, gt = GT, ge = GE, app = APP }) .ToArray(); 但是这个方法不推荐使用,真心的…… 首先这里的nextInt()的执行顺序会有编译器优化导致被打乱的可能性(不相信你可以把orderby子句提前一下) 其次Linq本身运行并不快,在zju的PAT官网,那个超级喜欢严格时限的地方,容易小幅度超时又无处争辩。 (当然在这里,把读入部分用传统for循环来就不会超时了,排序继续愉快的Linq——注意到了吗,人家自己支持多关键词排序) 如果感兴趣,Linq的高级用法请自己查询,以下是我在zju的官网上通过的代码(比较极限的卡了时间限制,不用Linq可以再宽松一点点): (另:这里的数据造的太随性了,所以Split方法还要加个参数:StringSplitOptions.RemoveEmptyEntries,不过浙大没这种数据多空格的糟糕习惯,有VS2010的考试场地你也不用背,intellisense会智能提示的) using System; using System.Text; using System.Collections.Generic; using E = System.Linq.Enumerable; using System.Linq; class Program { string[] tokens; int lastToken; int nextInt() { if (lastToken >= tokens.Length) { string tmp = Console.ReadLine(); tokens = tmp.Split(new char[] { ' ' }); lastToken = 0; } return int.Parse(tokens[lastToken++]); } Program() { tokens = new string[1]; lastToken = 1; int n = nextInt(), m = nextInt(), k = nextInt(); int[] maxAcc = new int[m]; for (int i = 0; i < m; i++) maxAcc[i] = nextInt(); List[] schoolAccept = new List[m]; for (int i = 0; i < m; i++) schoolAccept[i] = new List(); int[] ge = new int[n]; int[] gi = new int[n]; int[][] appli = new int[n][]; for (int i = 0; i < n; i++) { ge[i] = nextInt(); gi[i] = nextInt(); appli[i] = new int[k]; for (int j = 0; j < k; j++) appli[i][j] = nextInt(); } var studentList = ( from i in E.Range(0, n) let GE = ge[i] let GT = ge[i] + gi[i] let APP = appli[i] orderby GT descending, GE descending select new { id = i, gt = GT, ge = GE, app = APP }) .ToList(); int[] rank = new int[n]; for (int i = 0; i < n; i++) { var item = studentList[i]; var lastItem = studentList[i - 1<0?0:i-1]; if (i!=0 && lastItem.gt == item.gt && lastItem.ge == item.ge) rank[item.id] = rank[lastItem.id]; else rank[item.id] = i; for (int j = 0; j < k; j++) { int sel = item.app[j]; if (schoolAccept[sel].Count < maxAcc[sel] || rank[schoolAccept[sel].Last()] == rank[item.id]) { schoolAccept[sel].Add(item.id); break; } } } for (int i = 0; i < m; i++) { schoolAccept[i].Sort(); for (int j = 0; j < schoolAccept[i].Count; j++) { Console.Write((j == 0 ? "" : " ") + schoolAccept[i][j]); } Console.WriteLine(""); } } public static void Main(String[] args) { new Program(); } }
 
切换
撰写答案
扫描后移动端查看本题