Luogu 1093 - 奖学金 - [排序水题]
题目链接:https://www.luogu.org/problemnew/show/P1093
题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
$7$ $279$
$5$ $279$
这两行数据的含义是:总分最高的两个同学的学号依次是 $7$ 号、$5$ 号。这两名同学的总分都是 $279$ (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为 $7$ 的学生语文成绩更高一些。如果你的前两名的输出数据是:
$5$ $279$
$7$ $279$
则按输出错误处理,不能得分。
输入输出格式
输入格式:
共 $n+1$ 行。
第 $1$ 行为一个正整数 $n( \le 300)$,表示该校参加评选的学生人数。
第 $2$ 到 $n+1$ 行,每行有 $3$ 个用空格隔开的数字,每个数字都在 $0$ 到 $100$ 之间。第 $j$ 行的 $3$ 个数字依次表示学号为 $j-1$ 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 $1~n$(恰好是输入数据的行号减 $1$)。
所给的数据都是正确的,不必检验。
//感谢 黄小U饮品 修正输入格式
输出格式:
共 $5$ 行,每行是两个用空格隔开的正整数,依次表示前 $5$ 名学生的学号和总分。
输入输出样例
输入样例#1:
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
输出样例#1:
6 265
4 264
3 258
2 244
1 237
输入样例#2:
8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
输出样例#2:
8 265
2 264
6 264
1 258
5 258
题解:
考察对结构体的排序……没啥难度。
纯粹用来练练手写快排和归并排序。
AC代码(快排):
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n;
struct Stu{
int id;
int ch,ma,en;
bool operator>(const Stu& oth)const
{
if(ch+ma+en==oth.ch+oth.ma+oth.en)
{
if(ch==oth.ch) return id<oth.id;
else return ch>oth.ch;
}
else return ch+ma+en>oth.ch+oth.ma+oth.en;
}
}stu[maxn]; void QuickSort(Stu a[],int l,int r)
{
int i=l, j=r;
Stu p=a[rand()%(r-l+)+l];
while(i<=j)
{
while(a[i]>p) i++;
while(p>a[j]) j--;
if(i<=j) swap(a[i],a[j]), i++, j--;
}
if(l<j) QuickSort(a,l,j);
if(i<r) QuickSort(a,i,r);
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
stu[i].id=i;
scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
}
QuickSort(stu,,n);
for(int i=;i<=;i++) printf("%d %d\n",stu[i].id,stu[i].ch+stu[i].ma+stu[i].en);
}
AC代码(归并排序):
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n;
struct Stu{
int id;
int ch,ma,en;
bool operator>(const Stu& oth)const
{
if(ch+ma+en==oth.ch+oth.ma+oth.en)
{
if(ch==oth.ch) return id<oth.id;
else return ch>oth.ch;
}
else return ch+ma+en>oth.ch+oth.ma+oth.en;
}
}stu[maxn]; Stu tmp[maxn];
void MergeSort(Stu a[],int l,int r)
{
int mid=(l+r)>>;
if(l<mid) MergeSort(a,l,mid);
if(mid+<r) MergeSort(a,mid+,r);
int i=l, j=mid+, k=l;
while(i<=mid && j<=r) tmp[k++]=a[i]>a[j]?a[i++]:a[j++];
while(i<=mid) tmp[k++]=a[i++];
while(j<=r) tmp[k++]=a[j++];
memcpy(&a[l],&tmp[l],(r-l+)*sizeof(Stu));
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
stu[i].id=i;
scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
}
MergeSort(stu,,n);
for(int i=;i<=;i++) printf("%d %d\n",stu[i].id,stu[i].ch+stu[i].ma+stu[i].en);
}
Luogu 1093 - 奖学金 - [排序水题]的更多相关文章
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- PAT甲题题解-1062. Talent and Virtue (25)-排序水题
水题,分组排序即可. #include <iostream> #include <cstdio> #include <algorithm> #include < ...
- HDU排序水题
1040水题; These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fa ...
- hdu1106 排序水题
Problem Description 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整 ...
- UOJ #278. 【UTR #2】题目排列顺序(排序水题)
#278. [UTR #2]题目排列顺序 丢个传送门:http://uoj.ac/problem/278 描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 U ...
- hdu 5427(排序水题)
排序 年轻的排前面 名字中可能有空格 Sample Input21FancyCoder 19962FancyCoder 1996xyz111 1997 Sample OutputFancyCoderx ...
- hdu 5038 (2014北京网络赛G 排序水题)
题意:有n个数字,带入10000 - (100 - ai) ^ 2公式得到n个数,输出n个数中频率最大的数,如果有并列就按值从小到大都输出输出,如果频率相同的数字是全部的n个数,就输出Bad....题 ...
- hdu2083 简易版之最短距离 排序水题
给出数轴n个坐标,求一个点到所有点距离总和最小.排序后最中间一个点或两个点之间就是最优 #include<stdio.h> #include<algorithm> using ...
- hdoj--1379--DNA Sorting(排序水题)
DNA Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- MFC通过button控制编辑框是否显示系统时间
在dlg.h中public bool flag; 在构造函数中 flag=false; 在button的生成函数中 if(flag) { flag=false; //m_showtime.SetWin ...
- haproxy+keepalived(涵盖了lvs,nginx.haproxy比较)
文章转载自: haproxy+keepalived https://cloud.tencent.com/developer/article/1026385 网络四层和七层的区别 https: ...
- angularjs中的路由介绍详解 ui-route(转)
http://www.cnblogs.com/littlemonk/p/5500801.html 这篇文章主要介绍了Angularjs中UI Router全攻略,涉及到angularjs ui rou ...
- git学习小游戏
学习git可以试试这个游戏:https://learngitbranching.js.org/
- 重新粗推了一下Master Theorem
主定理一般形式是T(n) = a T(n / b) + f(n), a >= 1, b > 1.递归项可以理解为一个高度为 logbn 的 a 叉树, 这样 total operation ...
- 挖坑:hive集成kerberos
集成hive+kerberos前,hadoop已经支持kerberos,所以基础安装略去: https://www.cnblogs.com/garfieldcgf/p/10077331.html 直接 ...
- F3D模式规则详解
F3D有两个版本,长期版还有短期版 长期版规则 1.购买时候分配 第一队 20% to 奖金池, 56%分给所有人, 30% 持有p3d的人第二队 35% to 奖金池, 43%分给所有人, 8% 持 ...
- 建站工具Hexo
$ npm install hexo-cli -g $ hexo init blog $ cd blog $ npm install $ hexo server
- hdoj:2085
核反应堆 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- JAVA并发编程——守护线程(Daemon Thread)
在Java中有两类线程:用户线程 (User Thread).守护线程 (Daemon Thread). 所谓守护 线程,是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称 ...