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 ...
随机推荐
- 【2017下集美大学软工1412班_助教博客】团队作业3——需求改进&系统设计团队成绩公示
作业要求 团队作业3--需求改进&系统设计 团队评分结果 Total QH BZ GJ MS SXX WBS SJ JG SJK JH NR CS PHILOSOPHER 需求改进&系 ...
- 洛谷P1208
#include <iostream>#include <algorithm>#include <cstdio>using namespace std; struc ...
- PSR规范0-4整理
PSR规范 psr规范 引言: PSR 是 PHP Standard Recommendations 的简写,由 PHP FIG 组织制定的 PHP 规范,是 PHP 开发的实践标准.这些规范的目的是 ...
- Publisher和Subscriber节点
一.Publisher节点 /*"ros/ros.h"里面包含了ROS系统内最常用的一些头文件,包含此文件,便可以使用ROS的核心功能.*/#include "ros/r ...
- OpenCV——图像的深度与通道数讲解
矩阵数据类型: – CV_(S|U|F)C S = 符号整型 U = 无符号整型 F = 浮点型 E.g.: CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双 ...
- SQL 字符串分割表函数
--字符串分割表函数 ) ) declare @i int; declare @count int; ); ); declare @Index int; )) declare @rowID int; ...
- scapy学习笔记(2)--包及包的定义
转载请注明:@小五义:http://www.cnblogs/xiaowuyi 一.包 包(Packet)是TCP/IP协议通信传输中的数据单位,一般也称“数据包”.其主要由“目的IP地址”.“源IP地 ...
- awk、sed处理文件的简单例子
awk.sed对处理日志文件和写shell脚本时非常有益.这个东西,如果不经常操作,真心过一段时间就忘差不多..要掌握熟练,就要多练习,这没什么可说的. awk '条件{命令}' filename 假 ...
- Mysql客户端软件
Mysql客户端软件Navicat,使用起来很方便; PremiumSoft Navicat for MySQL Enterprise Edition v8.0.27姓名(Name):3ddown.c ...
- 2.3《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——重命名,复制,删除
最常用的文件操作除了将文件列出来外,就应该是重命名,复制,删除了.正如将文件列出来一样,大多数现代操作系统为这些任务提供了用户图形界面,但是在许多场景中,用命令行还是会更方便. 使用mv命令重命名一个 ...