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

C++语言 码拜 8年前 (2016-05-01) 1227次浏览
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入格式:
输入第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
本人设计的程序样例通过了,但提交,运行超时,本人快速排序和合并排序都试过了
本人感觉是,本人两个学生成绩的比较设计得太复杂了
有没有高手,做过这道题,成绩的比较,有没有不是嵌套的!
解决方案

10

不知道你是怎么实现的……本人的想法是先遍历一遍把每个学生分到题中所述的一二三四类,再对每个类进行排序。

40

#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个小类)。这都是牺牲空间来换取时间的做法。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明算法问题,运行超时了,求ACM给个思路
喜欢 (0)
[1034331897@qq.com]
分享 (0)