超时的话试一试将cin、cout换成scanf,printf
map换成unordered_map
map很好用
unordered_map可以避免超时!!
同类型题汇总:
1085 PAT单位排行 (25分)
1080 MOOC期终成绩
我的代码,不好意思是错的
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define max1 10010
#define max2 1000
struct info {
string testId;
int score;
}stu[max1];
struct result {
string testId;
int score;
}res[max1];
struct res3 {
int place;
int sum = 0;
}count3[max2];
bool cmp1(result a,result b){
if (a.score != b.score) {
return a.score > b.score;
}
else {
return a.testId < b.testId;
}
}
bool cmp2(res3 a, res3 b) {
return a.sum > b.sum;
}
int main() {
int n, m;
int i, j;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> stu[i].testId;
cin >> stu[i].score;
}
int type;//指令类型
string temp;//指令内容
string t; //缓存
int sum;
for (j = 0; j < n; j++) {
cin >> type;
cin >> temp;
cout << "Case " << j + 1 << ": " << type << " " << temp << endl;
if (type == 1) {
sum = 0;
for (i = 0; i < n; i++) {
t = stu[i].testId.substr(0, 1); //等级TAB
if (t.compare(temp) == 0) {
res[sum].testId = stu[i].testId;
res[sum].score = stu[i].score;
sum++;
}
}
sort(res, res + sum, cmp1);
for (int m = 0; m < sum; m++) {
cout << res[m].testId << " ";
cout << res[m].score << endl;
}
if (sum == 0) {
cout << "NA" << endl;
}
}
else if (type == 2) {
sum = 0;
int total = 0;
for (i = 0; i < n; i++) {
t = stu[i].testId.substr(1, 3); //等级TAB
if (t.compare(temp) == 0) {
sum++;
total += stu[i].score;
}
}
if (sum > 0) {
cout << sum << " " << total << endl;
}
else {
cout << "NA" << endl;
}
}
else if (type == 3) {
int m = 0;
sum = 0;
for (i = 0; i < n; i++) {
t = stu[i].testId.substr(4, 6); //等级TAB
if (t.compare(temp) == 0) {
sum++;
string test = stu[i].testId.substr(1, 3);
int tt = stoi(test);
count3[tt].place = tt;
count3[tt].sum++;
}
}
sort(count3, count3 + max2, cmp2);
for (i = 0; i < max2; i++) {
if (count3[i].sum == 0) {
break;
}
else {
cout << count3[i].place << " " << count3[i].sum << endl;
}
}
if (sum == 0) {
cout << "NA" << endl;
}
}
}
return 0;
}
人家的代码
一道超时让我怀疑人生的题目,一开始只用的c,19分,超时,直到现在也没想出来为啥,后来百度到用map容器解决关于第三种情况,但是敲了还是超时,崩溃,还是19分,想了半天,最后发现将所有的输出改成printf就对了,用cout的话,不行, 还有使用map的时候,因为map会自动将键值从小到大排序,然后还会导致超时,好像是22分,如果用无排序的map,25分
————————————————
版权声明:本文为CSDN博主「依久_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/acm_zh/java/article/details/86429864
#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
struct Node {
string t;
int value;
};
bool cmp(const Node& a, const Node& b) {
return a.value != b.value ? a.value > b.value : a.t < b.t;
}
int main() {
int n, m, score, num;
string s;
cin >> n >> m;
vector<Node> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].t >> v[i].value;
for (int i = 1; i <= m; i++) {
cin >> num >> s;
printf("Case %d: %d %s
", i, num, s.c_str());
vector<Node> ans;
int cnt = 0, score = 0;
if (num == 1) {
for (int j = 0; j < n; j++) {
if (v[j].t[0] == s[0]) ans.push_back(v[j]);
}
}
else if (num == 2) {
for (int j = 0; j < n; j++) {
if (v[j].t.substr(1, 3) == s) {
cnt++;
score += v[j].value;
}
}
if (cnt != 0) printf("%d %d
", cnt, score);
}
else if (num == 3) {
unordered_map<string, int> m;
for (int j = 0; j < n; j++) {
if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++;
}
for (auto it : m) ans.push_back({ it.first, it.second });
}
sort(ans.begin(), ans.end(), cmp);
for (int j = 0; j < ans.size(); j++) {
printf("%s %d
", ans[j].t.c_str(), ans[j].value);
}
if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0))
printf("NA
");
}
return 0;
}