1039. Course List for Student (25)
时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.Input Specification:Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.Output Specification:For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.Sample Input:11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
Sample Output:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0
用map和vector 充当数组邻接表的最后一个测试点运行超时,然后就用大数组了;还看到另一个通过数据范围和课程编号的Hash的,这个就直接贴别人的了,其实还有的没有完全理解好,那个id应该是默认开始为零吧,等有时间重新弄弄看,没有初始化总觉得乖乖的,那个异或48的好像是通过ASCII码直接取,相当于char-‘0’;。三个都在后面
2015-8-6 19:57理顺了Hash的
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月06日
13:23 | 答案正确 | 25 | 1039 | C++
(g++ 4.7.2) | 85 | 10924 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 3 | 4420 | 15/15 |
1 | 答案正确 | 3 | 4404 | 2/2 |
2 | 答案正确 | 3 | 4468 | 2/2 |
3 | 答案正确 | 3 | 4404 | 2/2 |
4 | 答案正确 | 3 | 4480 | 2/2 |
5 | 答案正确 | 85 | 10924 | 2/2 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
bool Cmpcourse(const int&A, const int&B){ return A < B; }
#define MAX 26*26*26*10
int indexreturn (char*name)
{
return (((name[0] - 'A') * 26 + name[1] - 'A') * 26 + name[2] - 'A') * 10 + name[3] - '0';
}
int main()
{
vector<vector<int> >studentGmap(MAX);/*这个vector是用括号的*/
int N, K,course,num,index;
char name[5];
scanf("%d%d", &N, &K);
while (K--)
{
scanf("%d%d",&course,&num);
while (num--)
{
scanf("%s",name);
studentGmap[indexreturn(name)].push_back(course);
}
}
while (N--)
{
scanf("%s", name);
index = indexreturn(name);
K = studentGmap[index].size();
printf("%s %d",name,K);
sort(studentGmap[index].begin(), studentGmap[index].end(), Cmpcourse);
for (course=0; course<K; course++)
printf(" %d", studentGmap[index][course]);
printf("\n");
}
system("pause");
return 0;
}
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月06日 19:55 | 答案正确 | 25 | 1039 | C++ (g++ 4.7.2) | 97 | 3892 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 2 | 3044 | 15/15 |
1 | 答案正确 | 2 | 2868 | 2/2 |
2 | 答案正确 | 2 | 2868 | 2/2 |
3 | 答案正确 | 2 | 2868 | 2/2 |
4 | 答案正确 | 2 | 2868 | 2/2 |
5 | 答案正确 | 97 | 3892 | 2/2 |
#include<iostream> #include<string> #include<algorithm> using namespace std;int nameToindex(char *name) { return (name[0]-'A')*26*260+(name[1]-'A')*260+(name[2]-'A')*10+name[3]-'0';}bool HashCMP(const int&A,const int &B){return A<B;}int binaryFind(int key,int Start,int End,int *studentHash){ int mid; while(Start<=End) { mid=(Start+End)/2; if(studentHash[mid]>key) End=mid-1; else Start=mid+1; } return End;}int main(){ int studentHash[2500*200]={0}; int studentIndex[26*26*26*10]={0}; int N, K, course, num,index,different,identity; char name[5]; scanf("%d%d",&N,&K); different=1; identity=0; while (K--) { scanf("%d%d",&course,&num); while (num--) { scanf("%s",name); index=nameToindex(name); if(0==studentIndex[index]) studentIndex[index]=different++;/*这里根据每个名字第一次进入进行划分,第一个1,第二个2,…… *那么下面studentHash[i]的值只要范围在[2501,5000]属于第一个的 范围在[5001,7500]属于第二个的 以此递增*/ studentHash[identity++]=studentIndex[index]*2500+course; } } sort(studentHash,studentHash+identity,HashCMP); while (N--) { scanf("%s",name); different=studentIndex[nameToindex(name)]; if(0==different) printf("%s 0\n",name); else { index=binaryFind(different*2500,0,identity-1,studentHash);/*获得的是小于或等于different*2500的,那么接下去的是[different*2500+1,(different+1)*2500]中最小的*/ num=binaryFind((different+1)*2500,index,identity-1,studentHash);/*获得的是小于或等于(different+1)*2500的*/ printf("%s %d",name,num-index); for(index++;index<=num;index++) printf(" %d",studentHash[index]-different*2500); printf("\n"); } } system("pause"); return 0;}
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月06日 16:50 | 答案正确 | 25 | 1039 | C++ (g++ 4.7.2) | 78 | 3840 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 1 | 308 | 15/15 |
1 | 答案正确 | 1 | 304 | 2/2 |
2 | 答案正确 | 1 | 308 | 2/2 |
3 | 答案正确 | 1 | 180 | 2/2 |
4 | 答案正确 | 1 | 180 | 2/2 |
5 | 答案正确 | 78 | 3840 | 2/2 |
#include <stdio.h>#include <map>#include <set>#include <string.h>#include <string>#include <algorithm>#include <vector>using namespace std;bool get(int& a){ if( scanf("%d",&a) == EOF ) return false ; return true ;}const int maxn = 2500*200 ;int nimei[maxn] , cnt ;int id[26][26][26][10] ;int bs(int key,int l,int r){ int mid ; while ( l <= r ) { mid = ( l + r ) >> 1 ; if( nimei[mid] > key ) r = mid - 1 ; else l = mid + 1 ; } return r ;}int main(){ int n , m ; get(n); get(m); int i , j , k ; int idCnt = 1 ; cnt = 0 ; for ( i = 0 ; i < m ; i++) { get(j); get(k); char str[16] ; while(k--) { scanf("%s",str); int &temp = id[str[0]-'A'][str[1]-'A'][str[2]-'A'][str[3]^48] ; if( temp == 0 ) temp = idCnt++; nimei[cnt++] = temp*2500+j ; } } sort(nimei,nimei+cnt); char name[16] ; while(n--) { scanf("%s",name); int t = id[name[0]-'A'][name[1]-'A'][name[2]-'A'][name[3]^48] ; if( t == 0 ) { printf("%s 0\n",name); continue; } j = bs(t*2500,0,cnt-1) ; k = bs((t+1)*2500,j,cnt-1); printf("%s %d",name,k-j); for ( j++; j <= k ; j++) { printf(" %d",nimei[j]-t*2500); } puts(""); } return 0;}
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月06日 10:56 | 部分正确 | 23 | 1039 | C++ (g++ 4.7.2) | 1 | 384 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 1 | 308 | 15/15 |
1 | 答案正确 | 1 | 304 | 2/2 |
2 | 答案正确 | 1 | 264 | 2/2 |
3 | 答案正确 | 1 | 180 | 2/2 |
4 | 答案正确 | 1 | 384 | 2/2 |
5 | 运行超时 | | | 0/2 |
#include<iostream> #include<string> #include<map> #include<vector>#include<algorithm>using namespace std; bool Cmpcourse(const int&A, const int&B){ return A < B; }int main(){ map<string, vector<int>>studentGmap; int N, K,course,num; string name; cin >> N >> K; while (K--) { cin >> course >> num; while (num--) { cin >> name; studentGmap[name].push_back(course); } } while (N--) { cin >> name; cout << name << " " << studentGmap[name].size(); sort(studentGmap[name].begin(), studentGmap[name].end(), Cmpcourse); for (vector<int>::iterator YB = studentGmap[name].begin(); YB != studentGmap[name].end(); YB++) cout <<" "<< (*YB); cout << endl; } system("pause"); return 0;}