首页 > 其他分享 >1047 Student List for Course【超简单思路,map,vector,对于超时问题】

1047 Student List for Course【超简单思路,map,vector,对于超时问题】

时间:2024-08-26 17:51:02浏览次数:7  
标签:map 1047 course List number cin courses each BOB5

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤40,000), the total number of students, and K (≤2,500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (≤20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.

Sample Input:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

关于第三个测试点超时:根据我改了半天,终于发现是换行所导致的。endl比\n要慢!!!(但我不知道会影响这么多,我先是把cin都改成scanf但是在字符串读取出现了问题!.......最后发现就是换行符所导致的超时!

( 1)加了    ios::sync_with_stdio(0);
            cin.tie(0);     运行时间如下:

(2)没加,会慢一些  但是影响不是很大 (3)没把endl换成'\n',就会导致第三个测试点超时!!!!

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

map<int, vector<string>> m;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, k, b, x;
    string a;

    cin >> n >> k;
    while (n--) {
        cin >> a >> b;
        for (int i = 0; i < b; i++) {
            cin >> x;
            m[x].push_back(a);
        }
    }

    for (int i = 1; i <= k; i++) {
        cout << i << ' ' << m[i].size() << '\n';
        sort(m[i].begin(), m[i].end());
        for(int j=0;j< m[i].size();j++){
         	cout<<m[i][j]<<'\n';
		} 
    }

    return 0;
}

标签:map,1047,course,List,number,cin,courses,each,BOB5
From: https://blog.csdn.net/m0_67413470/article/details/141567722

相关文章

  • 深信服安全服务认证工程师(SCSA-S)系列课程——渗透测试环境搭建与工具使用-Nmap
    Nmap简介Nmap是Linux下一款开源免费的网络发现(NetworkDiscovery)和安全审计(SecurityAuditing)工具,软件名字Nmap是NetworkMapper的简称。Nmap最初由Fyodor在1996年开始创建,随后在开源社区众多的志愿者参与下,该工具逐渐成为最为流行的安全必备工具之一。Nmap使用原始......
  • 使用ArraysList集合类实现新闻管理系统
    新闻管理系统,需求如下:        可以存储各类新闻标题(包含ID、名称、创建者)。        可以获取新闻标题的总数。        可以逐条打印每条新闻标题的名称。 News类:新闻类来存储新闻的相关信息,包括ID、名称和创建者。publicclassNews{publi......
  • ArrayList遍历, 元素查找
    1,ArrayList集合的遍历与数组类似,都可以使用foreach语句string[]str1={"a","b","c","d","e","f"};ArrayListList=newArrayList(str1);foreach(variteminList)......
  • ArrayList元素的删除(4种函数)
    1Clear()方法Clear()方法用来从ArrayList中移除所有元素,语法格式如下。string[]str1={"a","b","c"};ArrayListList=newArrayList(str1);List.Clear();2Remove()方法Remove()方法用来从ArrayList中移除特定对象的第一个......
  • ArrayList声明,Add(), Insert();
     ArrayList提供了3个构造器,通过这3个构造器可以有3种声明方式。(1)默认构造器,会以默认大小(16位)初始化内部数组。构造器格式如下。ArrayListList=newArrayList();//实例化一个ArrayList,命名为List;for(inti=0;i<10;i++)//添加10个元素......
  • 重塑列表美学:CSS `list-style` 属性的魔法
    重塑列表美学:CSSlist-style属性的魔法摘要CSS的list-style属性是控制列表项标记样式的强大工具。通过这个属性,开发者可以轻松改变无序列表和有序列表的外观,包括标记的类型、位置以及图像。本文将深入探讨list-style属性的各个方面,并提供丰富的代码示例,展示如何使用......
  • 1.MapReduce论文翻译
    MapReduce:SimplifiedDataProcessingonLargeClusters(MapReduce:简化大型集群下的数据处理)作者:JeffreyDeanandSanjayGhemawatAbstract(摘要)MapReduce是一个关于实施大型数据集处理和生成的编程模型。用户指定一个用于处理k/v对,生成中间态k/v集合的映射(map)函数,以及......
  • 2.MapReduce论文总结
    一.介绍很多业务逻辑很简单,主要难点是数据量太大,可使用分布式处理提高速度。传统分布式程序,计算逻辑和分布式任务分发、故障恢复混在一起,原本简单的计算逻辑变得模糊不清,难以处理。MapReduce将两者分离,任务分发,容错,恢复等逻辑由模型完成,程序员只需要专注计算逻辑。大大了简化......
  • Lab 1: MapReduce
    Lab1:MapReduce目标:实现一个MapReduce系统。其中包含:worker进程:调用Map和Reduce程序并处理文件的读写coordinator进程:负责将任务分发给worker并处理失败的worker。(注:本Lab使用coordinator而不是论文的master进行管理。)Gettingstartedsrc/main/mrsequential.go中提供......
  • 聊一聊 C# 中让人惶恐的 Bitmap
    一:背景1.讲故事在.NET高级调试的旅程中,我常常会与Bitmap短兵相接,它最大的一个危害就是会让程序抛出匪夷所思的OutOfMemoryException,也常常会让一些.NET开发者们陷入其中不能自拔,痛不欲生,基于此,这一篇我从dump分析的角度给大家深挖一下Bitmap背后的故事。二:Bitmap背后的......