用map会导致最后一个样例超时,改为unordered_map通过,详细建议参考https://blog.youkuaiyun.com/weixin_41256413/article/details/83720314
#include<algorithm>
#include<string>
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int main() {
int N, K;
scanf("%d %d", &N, &K);
int c, s;
string studentName;
unordered_map<string, vector<int> > stu;
for (int i = 0; i < K; i++) {
scanf("%d %d", &c, &s);
for (int j = 0; j < s; j++) {
cin >> studentName;
stu[studentName].push_back(c);
}
}
for (int i = 0; i < N; i++) {
cin >> studentName;
cout << studentName << " " << stu[studentName].size();
sort(stu[studentName].begin(), stu[studentName].end());
for (int k = 0; k < stu[studentName].size(); k++) {
printf(" %d", stu[studentName][k]);
}
printf("\n");
}
return 0;
}

本文分享了在算法竞赛中,如何将原本使用map的数据结构优化为unordered_map,以解决最后一个样例超时的问题。通过具体的代码示例,展示了如何在C++中实现这一优化,避免了map的对数时间复杂度,提高了查找效率。
582

被折叠的 条评论
为什么被折叠?



