1005 Graduate Admission&&QSORT的用法

题目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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值