Luogu 1093 - 獎學金 - [排序水題]

題目連接:https://www.luogu.org/problemnew/show/P1093c++

題目描述
某小學最近獲得了一筆贊助,打算拿出其中一部分爲學習成績優秀的前5名學生髮獎學金。期末,每一個學生都有3門課的成績:語文、數學、英語。先按總分從高到低排序,若是兩個同窗總分相同,再按語文成績從高到低排序,若是兩個同窗總分和語文成績都相同,那麼規定學號小的同窗 排在前面,這樣,每一個學生的排序是惟一肯定的。學習

任務:先根據輸入的3門課的成績計算總分,而後按上述規則排序,最後按排名順序輸出前五名名學生的學號和總分。注意,在前5名同窗中,每一個人的獎學金都不相同,所以,你必須嚴格按上述規則排序。例如,在某個正確答案中,若是前兩行的輸出數據(每行輸出兩個數:學號、總分) 是:ui

$7$ $279$
$5$ $279$spa

這兩行數據的含義是:總分最高的兩個同窗的學號依次是 $7$ 號、$5$ 號。這兩名同窗的總分都是 $279$ (總分等於輸入的語文、數學、英語三科成績之和) ,但學號爲 $7$ 的學生語文成績更高一些。若是你的前兩名的輸出數據是:code

$5$ $279$
$7$ $279$blog

則按輸出錯誤處理,不能得分。排序

輸入輸出格式
輸入格式:
共 $n+1$ 行。ci

第 $1$ 行爲一個正整數 $n( \le 300)$,表示該校參加評選的學生人數。get

第 $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=305;
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+1)+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=1;i<=n;i++)
    {
        stu[i].id=i;
        scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
    }
    QuickSort(stu,1,n);
    for(int i=1;i<=5;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=305;
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)>>1;
    if(l<mid) MergeSort(a,l,mid);
    if(mid+1<r) MergeSort(a,mid+1,r);
    int i=l, j=mid+1, 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+1)*sizeof(Stu));
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        stu[i].id=i;
        scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
    }
    MergeSort(stu,1,n);
    for(int i=1;i<=5;i++) printf("%d %d\n",stu[i].id,stu[i].ch+stu[i].ma+stu[i].en);
}
相關文章
相關標籤/搜索