L2-3 名人堂与代金券

题解

这题的话,每一个人都要占一个排名,即使排名并列了。

对于最后一个排名来说,即使人数超过了指定的k,也要加入。

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e4+10;
struct Node {
    char name[17];
    int sc;
}node[maxn];

bool cmp(const Node& a,const Node& b) {
    if (a.sc!=b.sc) {
        return a.sc>b.sc;
    }
    return strcmp(a.name,b.name)<0;
}

int main()
{
    int n,g,k,sum=0;
    scanf("%d%d%d",&n,&g,&k);
    for (int i=0;i<n;i++) {
        scanf("%s %d",node[i].name,&node[i].sc);
        if (node[i].sc>=g) {
            sum+=50;
        }
        else if (node[i].sc<g&&node[i].sc>=60) {
            sum+=20;
        }
    }
    sort(node,node+n,cmp);
    printf("%d\n",sum);
    int i=0;
    while (i<k&&i<n) {
        int step=0;
        while (i+step<n&&node[i].sc==node[i+step].sc) {
            printf("%d %s %d\n",i+1,node[i+step].name,node[i+step].sc);
            step++;
        }
        i+=step;
    }
    return 0;
}

数据

/*
4 99 1
aaaa 99
bbbb 99
cccc 99
dddd 99
*/

猜你喜欢

转载自www.cnblogs.com/xyqxyq/p/12350255.html