pat测试1015 德才论。运行超时 标库耗时过大,用cstdio

来源:互联网 发布:人工智能涨停 编辑:程序博客网 时间:2024/06/09 18:54

第一次写博客,还不太懂,还请见谅

1015德才论运行超时解决办法

原题目

1015. 德才论 (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Li

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

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

输入格式:

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

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

输出格式:

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

输入样例:
14 60 8010000001 64 9010000002 90 6010000011 85 8010000003 85 8010000004 80 8510000005 82 7710000006 83 7610000007 90 7810000008 75 7910000009 59 9010000010 88 4510000012 80 10010000013 90 9910000014 66 60
输出样例:
1210000013 90 9910000012 80 10010000003 85 8010000011 85 8010000004 80 8510000007 90 7810000006 83 7610000005 82 7710000002 90 6010000014 66 6010000008 75 7910000001 64 90
题目最初想来难度应该不会太大,但是最后超时严重

有两个地方可以优化时间问题,

1.不要用自己的排序函数,用sort重载版本

2.不要用标准库iostream,用cstdio来做。但是紧接着用了cstdio后vs2013下scanf报错,此时把scanf改为scanf_s编译既可以通过。但是pat平台反而出错。如果用scanf会vs报错,pat平台通过。

会报错错误 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.c:\code\pat\1015德才兼备\1015德才兼备\1015德才兼备.cpp 28 1 1015德才兼备

原因嘛,scanf没有越界等的检查,微软认为它很危险,所以呢,就把它改造成了一个有参数检查的版本。

最后附上自己的运行时间对比及代码

用cin cout时候第二三个均超时


网上一个用stdio的一位仁兄的。原文再次点击打开链接


我改进自己代码后,也就是换成stdio后


自己代码


#include<cstdio>//#include<iostream>#include<algorithm>#include<vector>//标准库函数中输入输出站时间非常长。用s语言的方法输入输出//scanf函数有越界风险,故而ide比如微软建议使用改进版本scanf_s();using namespace std;struct student{int id;int moral;int sch;};inline bool compare(student a, student b){if ((a.sch + a.moral) != (b.sch + b.moral))return a.moral + a.sch > b.moral + b.sch;else if (a.moral != b.moral)return a.moral > b.moral;elsereturn a.id < b.id;}void print(vector<student>::iterator beg, vector<student>::iterator end){for (vector<student>::iterator ite = beg; ite != end; ite++)printf("%d %d %d\n",(*ite).id,(*ite).moral,(*ite).sch);/*cout << (*ite).id << " " << (*ite).moral << " " << (*ite).sch << endl;*/}int main(){int n, l, h;scanf_s("%d %d %d", &n, &l, &h);//scanf("%d %d %d",&n,&l,&h);//cin >> n >> l >> h;student stu;vector<student> HH, HL, LL, other;for (int i = 0; i != n; i++){scanf_s("%d %d %d", &stu.id , &stu.moral , &stu.sch);//cin >> stu.id >> stu.moral >> stu.sch;if (stu.moral >= l&&stu.sch >= l)if (stu.moral >= h)if (stu.sch >= h)HH.push_back(stu);elseHL.push_back(stu);elseif (stu.moral >= stu.sch)LL.push_back(stu);elseother.push_back(stu);}sort(HH.begin(), HH.end(), compare);sort(HL.begin(), HL.end(), compare);sort(LL.begin(), LL.end(), compare);sort(other.begin(), other.end(), compare);printf("%d\n",HH.size() + HL.size() + LL.size() + other.size());print(HH.begin(), HH.end());print(HL.begin(), HL.end());print(LL.begin(), LL.end());print(other.begin(), other.end());return 0;}



0 0
原创粉丝点击