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 ...
随机推荐
- C# 利用VS自带的WSDL工具生成WebService服务类(转载)
WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲解了. 那么,既然有直接引用的方式,为什么还要通过WSDL生成呢? ...
- SDN期末
一.项目描述 负载均衡程序 二.小组分工 组名:我们真的很弱 组员:李佳铭.吴森杰.张岚鑫.薛宇涛.杨凌澜 三.代码演示及过程描述 四.课程总结
- 前端工程构建工具之Yeoman
一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ...
- 前端aes解密实战小结
很多人对于AES加密并不是很了解,导致互相之间进行加密解密困难. 本文用简单的方式来介绍AES在使用上需要的知识,而不涉及内部算法.最后给出例子来帮助理解AES加密解密的使用方法. AES的麻烦 相比 ...
- HTML常用标签大全
- 3532: [Sdoi2014]Lis
Description 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去 ...
- kali 2016.2安装及配置
之前安装过kali,现在换了台电脑重新安装一遍,顺便记录下来,因为面向新手所以会很详(luo)细(suo) 安装: 首先到官网去下载镜像文件:https://www.kali.org/download ...
- AOP实现拦截对象以及获取切入目标方法和注解
AOP实现拦截对象以及获取切入目标方法和注解 一.JoinPoint是什么? AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用 o ...
- 无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件
1.问题症状 已经处于Debug模式,运行时完全正常,但是一调试就出现对话框,显示出错信息:“无法找到“XXX.exe”的调试信息,或者调试信息不匹配.未使用调试信息生成二进制文件.” 2.解决方法 ...
- JAVA框架 Spring 注解注入
一.首先需要引入jar包:spring-aop-4.2.4.RELEASE.jar.(在spring解压包libs内). 二.如果注解方式注入依赖的对象,需要引用新的约束. 内的:xsd-config ...