算法问题,运行超时了,求ACM大神给个思路

zycxnanwang
博客专家认证
2016-07-15 11:58:45
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。

随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。

输出格式:

输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
输出样例:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

我设计的程序样例通过了,但提交,运行超时,我快速排序和合并排序都试过了
我感觉是,我两个学生成绩的比较设计得太复杂了
有没有大神,做过这道题,成绩的比较,有没有不是嵌套的!
...全文
326 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hooked 2016-07-17
  • 打赏
  • 举报
回复
做OJ还有个小trick,把所有 cin / cout 换成 scanf / printf, 会有意外收获。想知道为什么的话可以查看 std::ios_base::sync_with_stdio
引用 10 楼 xsklld 的回复:
啊,楼上的代码是调试前的代码,贴错了,重贴一遍(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'; } }
bluewanderer 2016-07-16
  • 打赏
  • 举报
回复
引用 4 楼 zycxnanwang 的回复:
[quote=引用 1 楼 bluewanderer 的回复:] 如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。
我按照你的思路,改了一下,时间提了一点,但是还是超时了,呜呜。[/quote] 如果你觉得2楼的思路不错的话,说明你并没达到我说的“别的都没弄错”的前提...
zycxnanwang 2016-07-16
  • 打赏
  • 举报
回复
引用 10 楼 xsklld的回复:
啊,楼上的代码是调试前的代码,贴错了,重贴一遍(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'; } }
谢了,我好好琢磨一下,自己写的代码老是超时 做ACM有没有要注意的问题来防止超时!
zycxnanwang 2016-07-16
  • 打赏
  • 举报
回复
zycxnanwang 2016-07-16
  • 打赏
  • 举报
回复
引用 1 楼 bluewanderer 的回复:
如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。
我按照你的思路,改了一下,时间提了一点,但是还是超时了,呜呜。
zycxnanwang 2016-07-16
  • 打赏
  • 举报
回复
引用 2 楼 xsklld 的回复:
不知道你是怎么实现的……我的想法是先遍历一遍把每个学生分到题中所述的一二三四类,再对每个类进行排序。
嗯,不错的思路,我试一下,关键时间限制为200ms,哎,太容易超了
xskxzr 2016-07-16
  • 打赏
  • 举报
回复
不知道你是怎么实现的……我的想法是先遍历一遍把每个学生分到题中所述的一二三四类,再对每个类进行排序。
xskxzr 2016-07-16
  • 打赏
  • 举报
回复
啊,楼上的代码是调试前的代码,贴错了,重贴一遍(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'; } }
xskxzr 2016-07-16
  • 打赏
  • 举报
回复
另外不能用C++ 11好不爽啊,这个OJ也太旧了吧。
xskxzr 2016-07-16
  • 打赏
  • 举报
回复
#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个小类)。这都是牺牲空间来换取时间的做法。
zycxnanwang 2016-07-16
  • 打赏
  • 举报
回复
引用 6 楼 bluewanderer的回复:
[quote=引用 4 楼 zycxnanwang 的回复:] [quote=引用 1 楼 bluewanderer 的回复:] 如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。
我按照你的思路,改了一下,时间提了一点,但是还是超时了,呜呜。[/quote] 如果你觉得2楼的思路不错的话,说明你并没达到我说的“别的都没弄错”的前提...[/quote] 别的都没弄错,是什么意思?按照二楼的思路 可以通过样例,还是超时!
bluewanderer 2016-07-16
  • 打赏
  • 举报
回复
如果你别的都没弄错的话,可以考虑先把不及格的排除了再排序,虽然可能不是最快的,不过如果他们用N多人只有几个人及格的样本坑人这样至少能过。

64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧