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

4 篇文章 0 订阅
2 篇文章 0 订阅

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

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 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
题目最初想来难度应该不会太大,但是最后超时严重

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

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;
	else
		return 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);
				else
					HL.push_back(stu);
			else
				if (stu.moral >= stu.sch)
					LL.push_back(stu);
				else
					other.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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值