PAT 1047. Student List for Course (25) Mark~

Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.

Sample Input:
10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5
Sample Output:
1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
 
大概意思就是排序输出,这道题卡了好久,第四个case一直过不了,从内存超限-》段错误-》运行超时,哎~~。先把第四个case运行超时的代码贴上来标记一下,什么时候想出办法了再解决。
#include <stdio.h>   
#include <map>   
#include <set>   
#include <string.h>   
#include <string>   
#include <algorithm>   
#include <vector>   
  
using namespace std;  

typedef struct info{
	char name[5];
	int course[80];
}info;

info ss[40001];
int cc[2501];

bool cmp(const info &a,const info &b){
	for(int i = 0;i < 5;i ++)
		if(a.name[i] != b.name[i])
			return a.name[i] < b.name[i];
}

int main(void)  
{  
	int N,K;
	scanf("%d %d",&N,&K);
	for(int i =0;i < N;i++){
		int sum;
		scanf("%s",ss[i].name);
		scanf("%d",&sum);
		while(sum){
			int a,b,c;
			scanf("%d",&a);
			b = a/32;
			c = a%32;
			ss[i].course[b] = 1 << (c-1)| ss[i].course[b];
			cc[a] ++;
			sum--;
		}
	}
	sort(ss,ss + N,cmp);
	for(int i = 1;i <= K;i++){
		int b,c;
		printf("%d %d\n",i,cc[i]);
		b = i/32;
		c = i%32;
		for(int j = 0;j < N;j++){
			if((ss[j].course[b]>>(c - 1)) & 1 == 1)
				printf("%s\n",ss[j].name);
		}
	}
	return 0;
} 

上面结构体中标记课程的用的是位运算,如果改成bool型数组,则内存超限。现在是超时,郁闷!!!

ac的代码:

/**
 * pat-1047: Student List for Course (25)
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 40000
#define K 2500
#define C 20

typedef struct
{
	char sname[5];
	int cnum;  // course num,注册的课程数目
	int cNO[C];  // course NO.,课程编号
}info;  // size of info: 5 + 4 + 4 * C = 89B 


info students[N+1]; //下标从1开始使用
int courses[K+1]; //下标从1开始使用
int stat[2501][1250]; //统计每个课程有哪些同学选了,40000个同学每个同学占1bit放在1250个int里

int cmp(const void *a, const void *b)
{
	int i;
	for(i = 0; i < 5; i++)
		if((*(info *)a).sname[i] != (*(info *)b).sname[i])
			return (*(info *)a).sname[i] - (*(info *)b).sname[i];
	return 0;
}


int main()
{
	int i, j, n, k, a, b, count;
	memset(courses, 0, sizeof(courses));
	memset(stat, 0, sizeof(stat));
	scanf("%d%d", &n, &k);
	for(i = 1; i <= n; i++)
	{
		scanf("%s%d", &((students[i].sname)[0]), &(students[i].cnum));
		for(j = 0; j < students[i].cnum; j++)
		{
			scanf("%d", &(students[i].cNO[j]));
			courses[students[i].cNO[j]]++;  //第一遍读入的时候,把courses数组计算好
		}
	} 
	if(n > 1)
		qsort(&(students[1]), n, sizeof(info), cmp);

	//统计
	for(i = 1; i <= n; i++)
	{
		a = i / 32;
		b = i % 32;
		if(!b)
		{
			b = 32;
			a -= 1;
		}
			
		for(j = 0; j < students[i].cnum; j++)
			stat[students[i].cNO[j]][a] = stat[students[i].cNO[j]][a] | (1 << (b - 1));
	} //统计,将结果放入stat数组,按位存

	//打印结果
	for(j = 1; j <= k; j++)
	{
		printf("%d %d\n", j, courses[j]);
		count = 0;
		for(i = 0; i < 1250; i++)
		{
			if(count >= courses[j])
				break;
			if(stat[j][i])
			{
				for(a = 1; a <= 32; a++)
				{
					if((stat[j][i] >> (a - 1)) & 1)
					{
						printf("%s\n", students[i*32+a].sname);
						count++;
					}
				}
			}
		}
	}		
	return 0;
}


 


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值