PAT甲级题解-1047. Student List for Course (25)-排序
一开始是建立了course[2501][40001]数组,存储每节课的学生编号
然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时
后来发现最多40000学生,每个学生最多选20门课,那么总共也就40000*20
所以直接就存储学生-课程的信息,然后排个序,按照课程从小到大,课程一样的话则按字典序
然后从头扫一遍即可,复杂度O(80000)
不过要注意一点,有些课可能并没有出现,所以要做个判断,输出x 0。
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <cstdio>
using namespace std; const int maxn=+;
char id_name[maxn][];
struct Stu{
char name[];
int course;
bool operator<(const Stu tmp)const{
if(course==tmp.course){
if(strcmp(name,tmp.name)<=)
return true;
else
return false;
}
else
return course<tmp.course;
}
}stu[maxn*];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
int idx=;
int c;
char name[];
int num[];
memset(num,,sizeof(num));
for(int i=;i<n;i++){
//cin>>stu[i].name;
scanf("%s",name);
scanf("%d",&c);
for(int j=;j<c;j++){
scanf("%d",&stu[idx].course);
strcpy(stu[idx].name,name);
num[stu[idx].course]++;
idx++;
}
//sort(stu[i].course,stu[i].course+stu[i].c);
}
sort(stu,stu+idx);
int last=,now=;
for(int i=;i<idx;i++){
if(i==){
now=stu[i].course;
for(int j=;j<now;j++)
printf("%d 0\n",j); //没有出现的课,也要输出0
printf("%d %d\n",stu[i].course,num[stu[i].course]);
printf("%s\n",stu[i].name);
last=stu[i].course;
}
else{
if(stu[i].course!=stu[i-].course){
now=stu[i].course;
for(int j=last+;j<now;j++)
printf("%d 0\n",j); //没有出现的课,也要输出0
printf("%d %d\n",stu[i].course,num[stu[i].course]);
printf("%s\n",stu[i].name);
last=stu[i].course;
}
else{
printf("%s\n",stu[i].name);
}
}
}
return ;
}
PAT甲级题解-1047. Student List for Course (25)-排序的更多相关文章
- 【PAT甲级】1047 Student List for Course (25 分)
题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输 ...
- PAT 解题报告 1047. Student List for Course (25)
1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...
- PAT (Advanced Level) 1047. Student List for Course (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)
1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cou ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
随机推荐
- MySQL SELECT语句中只能输出1000行数据的原因
同事反映,客户的一套MySQL生产库,执行SELECT.. INTO OUTFILE语句只能导出1000行 最初以为是系统参数被重新设置了,建议他更改系统参数 mysql> set global ...
- 深入浅出SharePoint2013——Search Schema配置
如果增加了新的Metadata,请上传文件到文档库后,并确保每个字段都填充了数据.然后执行全文爬网. 这样才会产生对应新的管理属性.
- mnist手写数字检测
# -*- coding: utf-8 -*- """ Created on Tue Apr 23 06:16:04 2019 @author: 92958 " ...
- Articulate Presenter文字乱码的排除
Articulate Presenter乱码的问题如何设置? 字体乱码的设置: 1.首先如果ppt中有中文内容,肯定需要将Articulate Presenter的Character Set设置为No ...
- BZOJ2306:[CTSC2011]幸福路径(倍增Floyd)
Description 有向图 G有n个顶点 1, 2, …, n,点i 的权值为 w(i).现在有一只蚂蚁,从给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条边,它 ...
- [SDOI2008]洞穴勘测
嘟嘟嘟 写完lct的板儿后觉得这就是一道大水题. 连pushup都不用. 不过还是因为一个zz的错误debug了一小会儿(Link的时候连出自环--) 还有一件事就是Cut的时候判断条件还得加上,因为 ...
- [JSOI2008]星球大战starwar
嘟嘟嘟 维护联通块自然想到并查集,然而题中说是删边,不是很好做,因此我们可以离线下来然后倒序操作,就变成了添加边的同时维护联通块数量. 首先我们把k次打击后剩的边都添加到图中,表示倒序时的初始状态.然 ...
- mysql用户创建与授权
一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...
- java.lang.NoSuchMethodError: No static method getFont
最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...
- 在Node.js中操作文件系统(一)
在Node.js中操作文件系统 在Node.js中,使用fs模块来实现所有有关文件及目录的创建,写入及删除操作.在fs模块中,所有对文件及目录的操作都可以使用同步与异步这两种方法.比如在执行读文件操作 ...