PTA天梯赛L2-027 名人堂与代金券【排序】

对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,总评成绩必须达到 60 分及以上,并且有另加福利:总评分在 [G, 100] 区间内者,可以得到 50 元 PAT 代金券;在 [60, G) 区间内者,可以得到 20 元PAT代金券。全国考点通用,一年有效。同时任课老师还会把总评成绩前 K 名的学生列入课程“名人堂”。本题就请你编写程序,帮助老师列出名人堂的学生,并统计一共发出了面值多少元的 PAT 代金券。

输入格式:

输入在第一行给出 3 个整数,分别是 N(不超过 10 000 的正整数,为学生总数)、G(在 (60,100) 区间内的整数,为题面中描述的代金券等级分界线)、K(不超过 100 且不超过 N 的正整数,为进入名人堂的最低名次)。接下来 N 行,每行给出一位学生的账号(长度不超过15位、不带空格的字符串)和总评成绩(区间 [0, 100] 内的整数),其间以空格分隔。题目保证没有重复的账号。

输出格式:

首先在一行中输出发出的 PAT 代金券的总面值。然后按总评成绩非升序输出进入名人堂的学生的名次、账号和成绩,其间以 1 个空格分隔。需要注意的是:成绩相同的学生享有并列的排名,排名并列时,按账号的字母序升序输出。

输入样例:

10 80 5
[email protected] 78
[email protected] 87
[email protected] 65
[email protected] 96
[email protected] 39
[email protected] 87
[email protected] 80
[email protected] 88
[email protected] 80
[email protected] 70

输出样例:

360
1 [email protected] 96
2 [email protected] 88
3 [email protected] 87
3 [email protected] 87
5 [email protected] 80
5 [email protected] 80

思路:这题主要就是结构体排序,用sort非常方便,对sort不太熟悉的可以看看我之前写的关于sort排序技巧的博客链接 

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
struct node
{
    string s;
    int cow;
    bool operator < (const node &r) const
    {
        if(cow == r.cow)
            return s < r.s;
        return cow > r.cow;
    }
}a[maxn];
int main()
{
    int n, g, k;
    scanf("%d%d%d", &n, &g, &k);
    int sum = 0;
    for(int i = 0; i < n; ++i)
    {
        cin >> a[i].s >> a[i].cow;
        if(a[i].cow >= g)
            sum += 50;
        else if(a[i].cow >= 60 && a[i].cow < g)
            sum += 20;
    }
    sort(a, a + n);
    cout << sum << endl;
    int cnt = 1, tmp = 1;
    for(int i = 0; i < n; ++i)
    {
        if(i > 0 && a[i].cow == a[i - 1].cow)
            tmp++;
        if(i > 0 && a[i].cow != a[i - 1].cow)
        {
            cnt += tmp;
            tmp = 1;
        }
        if(cnt > k)
            break;
        cout << cnt;
        cout << " " << a[i].s << " " << a[i].cow << endl;
        
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/88650434