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 ...
随机推荐
- sql注入--基础
什么是sql注入: 利用SQL语句 在外部 对数据库进行 查询,更新等 动作 sql注入的原理: 输入值可控且带入数据库执行(前提) 接受的变量传值未进行过滤(实质) sql注入的目的: 获取数据(网 ...
- SpringBoot部署
Spring Boot 部署到服务器 jar 形式 1.打包 若我们在新建Spring Boot 项目的时候,选择打包方式是 jar,则我们只需要用 mvn package 就可以进行打包. 2.运行 ...
- Dijkstra(最短路求解)
Dijkstra(最短路求解) 模板: #include<iostream> #include<cstdio> #include<cstring> #include ...
- 【Alpha Go】Day 1 !
[Alpha Go]Day 1 ! Part 0 · 简要目录 Part 1 · 任务分配 Part 2 · 他日安排 Part 3 · 预期任务量 Part 4 · 团队贡献值计算原则 Part 1 ...
- 技术分享之AQS——内容提要
1. 背景 最近团队内部技术分享,我做了个关于AQS的分享.ppt中涵盖的部分要点内容,现在整理到博客上. 关于AQS本身的源码解读,可以参考我之前的博文. 2. 要点梳理 下面是一些技术分享的要点梳 ...
- AlloyDesigner 使用
前端开发视觉是很重要的一部分,所以视觉的还原度很重要,今天给大家介绍一个很好用的视觉精度调整插件 一.下载AlloyDesigner插件 下载插件 二.保存视觉稿为图片格式 1.psd 用ps直接保存 ...
- Opatching PSU in Oracle Database 11g Release 2 RAC on RHEL6
Opatching PSU in Oracle Database 11g Release 2(11.2.0.4) RAC on RHEL6 1) 升级opatch工具 1.1) For GI home ...
- (四) DRF认证, 权限, 节流
一.Token 认证的来龙去脉 摘要 Token 是在服务端产生的.如果前端使用用户名/密码向服务端请求认证,服务端认证成功,那么在服务端会返回 Token 给前端.前端可以在每次请求的时候带上 To ...
- 第12章 GPIO输入—按键检测
第12章 GPIO输入—按键检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...
- Swift图书展示项目笔记
1.Swift语言特点 Extensions(扩展):就是向一个已有的类.结构体.枚举类型或者协议类型添加新功能.这包括在没有权限获取原始源代码的情况下扩展类型的能力(即逆向建模) map: 得到一个 ...