1005 Graduate Admission&&QSORT的用法_想看焰火吗的博客-程序员秘密

技术标签: 9度OJ  

题目1005:Graduate Admission

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2428

解决:686

题目描述:

    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

另一组测试样例:

5 2 2
1 1
100 90 1 0
90 100 1 0
100 90 1 0
90 100 1 0
100 90 1 0

输出结果

1 3

0 2 4

俺的代码:

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string.h>  
using namespace std; 


struct stu{
int SID;
int GE;
int GI;
int G;
int preschoolId[6];
};
struct scl{
//int OID;
int maxnm;
int realnum;
int stdid[1000];
};
//排序函数
int cmp(const void *a, const void *b){
struct stu *c = (stu *)a;
struct stu *d = (stu *)b;
if(c->G != d->G){
return d->G - c->G;
}
else if(c->GE != d->GE){
return d->GE - c->GE;
}
}
int cmp2(const void *a,const void *b){
return *(int *)a - *(int *)b;
}
int main(int argc, char* argv[])
{
int N,M,K;
int i,j,t,q;
scl aaa[100];//学校
stu bbb[1000];//学生
int tmp;
int flag=0;
int g,ge;
while(scanf("%d %d %d",&N,&M,&K)!=EOF)
{
for(i=0;i<M;i++)
{
aaa[i].realnum=0;
scanf("%d",&aaa[i].maxnm);//最大录取人数
}
for(j=0;j<N;j++)
{
scanf("%d %d",&bbb[j].GE,&bbb[j].GI);//成绩
bbb[j].G = bbb[j].GE + bbb[j].GI;
bbb[j].SID=j;
for(t=0;t<K;t++)
{
scanf("%d",&bbb[j].preschoolId[t]);//志愿
}
}
qsort(bbb,N,sizeof(bbb[0]),cmp);
//printf("AFTET SORT*****************\n");
//for(j=0;j<N;j++)
//{
// printf("GE= %d GI= %d ID=%d\n",bbb[j].GE,bbb[j].GI,bbb[j].SID); 

//}
for(j=0;j<N;j++)
{
for(t=0;t<K;t++)
{
tmp = bbb[j].preschoolId[t];//第j个学生感兴趣的第t个学校
if(aaa[tmp].realnum<aaa[tmp].maxnm)
{
aaa[tmp].stdid[aaa[tmp].realnum]=bbb[j].SID;
aaa[tmp].realnum++;
//printf("第%d个学生被%d学校录取,id=%d!\n",j,tmp,bbb[j].SID);
break;
}


if(aaa[tmp].realnum>=aaa[tmp].maxnm)//特殊情况允许扩招
{
for(i=0;i<aaa[tmp].realnum;i++)
{
for(q=0;q<N;q++)//根据ID找i
{
if(bbb[q].SID==aaa[tmp].stdid[i])
{
ge=bbb[q].GE;
g=bbb[q].G;
break;
}
}
if((bbb[j].G==g)&&(bbb[j].GE==ge))
{
aaa[tmp].stdid[aaa[tmp].realnum]=bbb[j].SID;
aaa[tmp].realnum++;
//printf("KUOZHAO第%d个学生被%d学校录取,id=%d!\n",j,tmp,bbb[j].SID);
flag=1;
break;
}
}
if(flag==1)
{
flag=0;
break;
}
}
}
}
//printf("AFTET ZHAOSHENG*****************\n");
for(j=0;j<M;j++)
{
qsort(aaa[j].stdid,aaa[j].realnum,sizeof(aaa[j].stdid[0]),cmp2);
}
for(i=0;i<M;i++)
{
if(aaa[i].realnum==0) ;
else if(aaa[i].realnum==1) printf("%d",aaa[i].stdid[0]);
else if(aaa[i].realnum>1)
{
for(j=0;j<aaa[i].realnum-1;j++)
{
printf("%d ",aaa[i].stdid[j]);
}
printf("%d",aaa[i].stdid[aaa[i].realnum-1]);
}
printf("\n");
}
}
return 0;
}

QSORT的用法:

大神博客http://www.cnblogs.com/zhangshu/archive/2011/05/20/2052359.html

功 能: 使用 快速排序例程进行排序
头文件:stdlib.h
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数: 1 待排序 数组首地址
2 数组中待排序元素数量
3 各元素的占用空间大小
4 指向函数的 指针,用于确定排序的顺序

对一维数组的排序实例(从小到大排序):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<stdlib.h>
int  comp( const  void *a, const  void *b)
{
return  *( int *)a-*( int *)b;
}
int  main()
{
int  *array;
int  n;
scanf ( "%d" ,&n);
array=( int *) malloc (n* sizeof ( int ));
int  i=0;
for (;i<n;i++)
{
scanf ( "%d" ,(array+i));
}
qsort (array,n, sizeof ( int ),comp);
for (i=0;i<n;i++)
{
printf ( "%d\t" ,array[i]);
}
return0;
}

对一个 二维数组进行排序:
int a[1000][2]; 其中按照a[0]的大小进行一个整体的排序,其中a[1]必须和a[0]一起移动交换。//即第一行和第二行(a[0]和a[1]分别代表第一行和第二行的首地址)。使用库函数排序的代码量并不比用冒泡排序法小,但速度却快很多。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
qsort (a,1000, sizeof ( int )*2,comp);
 
int  comp( const  void *a, const  void *b)
 
{
return (( int *)a)[0]-(( int *)b)[0];
}
 
 
对字符串进行排序
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp (( char *)p2,( char *)p1);
}
int  main()
{
char  a[MAX1][MAX2];
initial(a);
qsort (a,lenth, sizeof (a[0]),Comp);
//lenth为数组a的长度
按结构体中某个关键字排序(对结构体一级排序):
structNode
{
double  data;
int  other;
}s[100];
int  Comp(constvoid*p1,constvoid*p2)
{
return (*(Node*)p2).data>(*(Node*)p1).data?1:-1;
}
qsort (s,100, sizeof (s[0]),Comp);
按结构体中多个关键字排序(对结构体多级排序)[以二级为例]:
struct  Node
{
int  x;
int  y;
}s[100];
//按照x从小到大排序,当x相等时按y从大到小排序
int  Comp( const  void *p1, const  void *p2)
{
struct  Node*c=(Node*)p1;
struct  Node*d=(Node*)p2;
if (c->x!=d->x)returnc->x-d->x;
else  return  d->y-c->y;
}
对结构体中字符串进行排序:
struct  Node
{
int  data;
char  str[100];
}s[100];
//按照结构体中字符串str的字典序排序
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp ((*(Node*)p1).str,(*(Node*)p2).str);
}
qsort (s,100, sizeof (s[0]),Comp);

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012530451/article/details/47300207

智能推荐

Logistic regression in R_吃机智豆长大的少女乙的博客-程序员秘密

Datacamp LearningLogistic regression in RBuilding simple logistic regression modelsThe donors dataset contains 93,462 examples of people mailed in a fundraising solicitation for paralyzed military...

看门狗驱动程序_鹤冲云霄的博客-程序员秘密

1. 看门狗的框架介绍看门狗是以misc设备的模式注册到设备中的,没有像其它设备那样复杂struct miscdevice { int minor; //此设备号 const char *name; //混杂设备名字 const struct file_operations *fops; //设备

java.lang.SecurityException: com.example.rxtest was not granted this permission: android.permission_牛八少爷的博客-程序员秘密

1. 错误描述java.lang.SecurityException: com.example.rxtest was not granted this permission: android.permission.WRITE_SETTINGS.java.lang.SecurityException: com.example.rxtest was not granted this permission: android.permission.WRITE_SETTINGS. at android.

PAT乙级 1007 素数对猜想 (20 分) C语言_Ronaldo777777的博客-程序员秘密

一、题目二、源代码#include&lt;stdio.h&gt;#include&lt;math.h&gt;int main(){ int n,i,j,temp,k=0,count=0,s[100000]; scanf("%d",&amp;n); for(i=2;i&lt;=n;i++) { temp=(int)sqrt((double)i); for(j=2;j&lt;=temp;j++) {

net framework 3.5 3.5官方完整版_ldy721224的博客-程序员秘密

net framework 3.5是在Windows运行的,一个针对.net开发的一个开发环境,也就是一个平台。.net程序开发必要的一款编程软件。我们都知道在这个越来越发达的信息时代,互联网将代替一切,应用程序将成为人们的最大发展潜能。现在搞开软件程序开发的人多之又多,小编也是学的开发,也用过各种软件写代码,编程序。net framework 3.5这款软件是.net的一款核心开发软件,很多.n...

2014年计算机一级考试试题,2014年最新全国计算机一级考试试题(1)_兔子的胡萝卜的博客-程序员秘密

ms 计算机一级2014年最新全国计算机一级考试试题1、在计算机内部用来传送、存储、加工处理的数据或指令都是以______形式进行的。A、十进制码 B、二进制码C、八进制码D、十六进制码答案:(B、)评析:在计算机内部用来传送、存储、加工处理的数据或指令都是以二进制码形式进行的。2、磁盘上的磁道是______。A、一组记录密度不同的同心圆B、一组记录密度相同的同心圆C、一条...

随便推点

简易爬虫,selenium的webdriver爬取上海python岗位信息_爬取上海公司信息_baibai_-的博客-程序员秘密

python实现selenium模拟人的行为使用谷歌浏览器打开拉勾网,输入python并点击上海按钮,爬取前25页简易数据,存储在txt文本里,(新手,请见谅)import timefrom selenium import webdriverfrom scrapy.selector import Selectorfrom selenium.webdriver.support.wait i...

校园网及入网计算机管理制度,校园网管理制度_李子寒泉的博客-程序员秘密

1、校园网仅用于教学、学习、科研和管理等活动,严禁利用网络从事违反国家法律法规、危害国家安全、泄露国家机密、干扰其它网络用户、侵犯知识产权等活动,不得利用校园网及计算机登陆不良网站。2、不得损坏、拆卸、移动、侵占校园网在校园内的设备、设施和线路,安置在各单位内的校园网设备、设施和线路,确因工作原因需要移动的,必须报告校园网络中心根据技术要求和实际情况进行移动。学校的所有信息系统的相关设备、系统和软...

java中调用Python__C中调用Python_c调用python中出现public static void main_ShirleyPaul的博客-程序员秘密

转载自:http://blog.sina.com.cn/s/blog_64e467d60100uhls.html http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录1.在Java类中直接执行Python语句 view plain import javax.script.*; import org.pyt

PAT乙级-链表-1025 反转链表 (25分)_pat乙级链表_544_的博客-程序员秘密

1025 反转链表 (25分)给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。输入格式:每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤10​5​​)、以及正整数 K (≤N),即要求反转的子链结点的个数。结点的地址是 5 位非负整数,NU

WinForm控件之【RichTextBox】_weixin_30614109的博客-程序员秘密

基本介绍高级文本控件,提供高级文本输入和编辑功能,如字符或段落格式的设置。常设置属性DetectUrls:指示是否自动将URL的格式设置为链接;EnableAutoDragDrop:是否启用文本、图片和其他数据的拖放操作;BorderStyle:指示编辑控件是否应带有边框或边框类型;Lines:多行编辑中的文本行,作为字符串值的数组;MaxLength:指定可...

一山容二虎,支持最新版Log4Net 的模板引擎-NVeloCity_weixin_30685029的博客-程序员秘密

在最近的ICMS项目中,为了实现完全的分离业务和视图部分(同时也需要视图偶尔能调用业务逻辑用用),所以决定引入已经非常成熟的开源搜索引擎Nvelocity. 但很可惜的是Nvelocity 的最新版本0.4.2 也在2003 年停止了更新和维护,其中引用的其他开源组件也都是当时的版本,比如: Log4Net版本是2002 年的 1.2.0.(而今的Nhibernate 3中自带的Log4...

推荐文章

热门文章

相关标签