64,641
社区成员
发帖
与我相关
我的任务
分享
[quote=引用 1 楼 bluewanderer 的回复:] 如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。
啊,楼上的代码是调试前的代码,贴错了,重贴一遍(CSDN不能编辑自己的回复真不方便)。 #include <algorithm> #include <iostream> #include <vector> using namespace std; struct Student { unsigned long number; unsigned morality_score, talent_score; }; bool is_greater(const Student &lhs, const Student &rhs) { unsigned lhs_total_score = lhs.morality_score + lhs.talent_score, rhs_total_score = rhs.morality_score + rhs.talent_score; if (lhs_total_score > rhs_total_score) return true; else if (lhs_total_score < rhs_total_score) return false; else if (lhs.morality_score > rhs.morality_score) return true; else if (lhs.morality_score < rhs.morality_score) return false; else if (lhs.number < rhs.number) return true; else return false; } int main() { unsigned long n; unsigned l, h; cin >> n >> l >> h; vector<Student> category[4]; Student student; for (unsigned long i = 0; i != n; ++i) { cin >> student.number >> student.morality_score >> student.talent_score; if (student.morality_score >= h && student.talent_score >= h) category[0].push_back(student); else if (student.morality_score >= h && student.talent_score >= l) category[1].push_back(student); else if (student.morality_score >= student.talent_score && student.talent_score >= l) category[2].push_back(student); else if (student.morality_score >= l && student.talent_score >= l) category[3].push_back(student); } cout << category[0].size() + category[1].size() + category[2].size() + category[3].size() << '\n'; for (unsigned i = 0; i != 4; ++i) { sort(category[i].begin(), category[i].end(), is_greater); for (vector<Student>::iterator it = category[i].begin(); it != category[i].end(); ++it) cout << it->number << ' ' << it->morality_score << ' ' << it->talent_score << '\n'; } }
如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。
不知道你是怎么实现的……我的想法是先遍历一遍把每个学生分到题中所述的一二三四类,再对每个类进行排序。
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct Student { unsigned long number; unsigned morality_score, talent_score; }; bool is_greater(const Student &lhs, const Student &rhs) { unsigned lhs_total_score = lhs.morality_score + lhs.talent_score, rhs_total_score = rhs.morality_score + rhs.talent_score; if (lhs_total_score > rhs_total_score) return true; else if (lhs_total_score < rhs_total_score) return false; else if (lhs.morality_score > rhs.morality_score) return true; else if (lhs.morality_score < rhs.morality_score) return false; else if (lhs.number < rhs.number) return true; else return false; } int main() { unsigned long n; unsigned l, h; cin >> n >> l >> h; vector<Student> category[4]; Student student; for (unsigned long i = 0; i != n; ++i) { cin >> student.number >> student.morality_score >> student.talent_score; if (student.morality_score >= h && student.talent_score >= h) category[0].push_back(student); else if (student.morality_score >= h && student.talent_score >= l) category[1].push_back(student); else if (student.morality_score >= student.talent_score && student.talent_score >= l) category[2].push_back(student); else if (student.morality_score >= l && student.talent_score >= l) category[3].push_back(student); } cout << category[0].size() + category[1].size() + category[2].size() + category[3].size() << '\n'; for (unsigned i = 0; i != 4; ++i) { sort(category[i].begin(), category[i].end(), is_greater); for (vector<Student>::iterator it = category[i].begin(); it != category[i].end(); ++it) cout << it->number << ' ' << it->morality_score << ' ' << it->talent_score << '\n'; } }
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Student {
unsigned long number;
unsigned morality_score, talent_score;
};
bool is_greater(const Student &lhs, const Student &rhs)
{
unsigned lhs_total_score = lhs.morality_score + lhs.talent_score,
rhs_total_score = rhs.morality_score + rhs.talent_score;
if (lhs_total_score > rhs_total_score) return true;
else if (lhs_total_score < rhs_total_score) return false;
else if (lhs.morality_score > rhs.morality_score) return true;
else if (lhs.morality_score < rhs.morality_score) return false;
else if (lhs.number > rhs.number) return true;
else return false;
}
int main()
{
unsigned long n;
unsigned l, h;
cin >> n >> l >> h;
vector<Student> category[3];
Student student;
for (unsigned long i = 0; i != n; ++i) {
cin >> student.number >>
student.morality_score >>
student.talent_score;
if (student.morality_score >= h && student.talent_score >= h)
category[0].push_back(student);
else if (student.morality_score >= h && student.talent_score >= l)
category[1].push_back(student);
else if (student.morality_score >= student.talent_score &&
student.talent_score >= l)
category[2].push_back(student);
}
cout << category[0].size() + category[1].size() + category[2].size() << '\n';
for (unsigned i = 0; i != 3; ++i) {
sort(category[i].begin(), category[i].end(), is_greater);
for (vector<Student>::iterator it = category[i].begin();
it != category[i].end(); ++it)
cout << it->number << ' ' << it->morality_score << ' ' <<
it->talent_score << '\n';
}
}
根据我的思路这段代码可以通过测试,不过时间最多有185ms,确实有点紧。
还有一些优化方案,比如vector里只存一个指针,这样sort执行时交换只用交换指针。
比如每个类再细分成(201-2l)个小类(代表总分2l~200),在分类的时候顺便根据总分扔进不同的小类;或者更彻底的,细分成(201-2l)*(101-l)个小类(每个小类代表一个(总分,德分)对,比如(a,b)可以放到第a*(101-l)+b个小类)。这都是牺牲空间来换取时间的做法。[quote=引用 4 楼 zycxnanwang 的回复:] [quote=引用 1 楼 bluewanderer 的回复:] 如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。