PAT甲题题解-1012. The Best Rank (25)-排序水题

排序,水题
因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E
那么按这个优先级顺序进行排序
每次排序前先求当前课程的排名
然后再与目前最好的排名比较、更新

至于查询,建立id与索引的映射即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int maxn=2000+5;
int n,m;
map<string,int> maps;
struct Stu{
    char id[10];
    int score[4];
    int ranks;
    int best_rank=5000;
    int c;
}stu[maxn];

bool cmp1(Stu a,Stu b){
    return a.score[0]>b.score[0];
}
bool cmp2(Stu a,Stu b){
    return a.score[1]>b.score[1];
}
bool cmp3(Stu a,Stu b){
    return a.score[2]>b.score[2];
}
bool cmp4(Stu a,Stu b){
    return a.score[3]>b.score[3];
}

void solveRanks(int k){
    stu[0].ranks=1;
    if(stu[0].ranks<stu[0].best_rank){
        stu[0].best_rank=stu[0].ranks;
        stu[0].c=k;
    }
    for(int i=1;i<n;i++){
        if(stu[i].score[k]==stu[i-1].score[k]){
            stu[i].ranks=stu[i-1].ranks;
        }
        else{
            stu[i].ranks=i+1;
        }
        if(stu[i].ranks<stu[i].best_rank){
            stu[i].best_rank=stu[i].ranks;
            stu[i].c=k;
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s %d %d %d",stu[i].id,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
        stu[i].score[3]=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
    }
    sort(stu,stu+n,cmp4);
    solveRanks(3);

    sort(stu,stu+n,cmp1);
    solveRanks(0);

    sort(stu,stu+n,cmp2);
    solveRanks(1);

    sort(stu,stu+n,cmp3);
    solveRanks(2);

    string str;
    for(int i=0;i<n;i++){
        str=stu[i].id;
        maps[str]=i+1;
    }
    char course[4]={‘C‘,‘M‘,‘E‘,‘A‘};
    for(int i=0;i<m;i++){
        cin>>str;
        int idx=maps[str]-1;
        if(idx==-1)
            printf("N/A\n");
        else
            printf("%d %c\n",stu[idx].best_rank,course[stu[idx].c]);
    }
    return 0;
}

时间: 2024-10-12 13:21:17

PAT甲题题解-1012. The Best Rank (25)-排序水题的相关文章

PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚

n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=30; int a[maxn]; int n,b; int main() { scanf("%d %d&q

PAT甲题题解-1052. Linked List Sorting (25)-排序

三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include <algorithm> #include <cstdio> #include <string.h> using namespace std; const int maxn=100000+5; struct Node{ int addr; int val; int to; bo

1012. The Best Rank (25)——PAT (Advanced Level) Practise

题目信息: 1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Math

PAT 1012. The Best Rank (25)

1012. The Best Rank (25) To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, w

[PTA] PAT(A) 1012 The Best Rank (25 分)

目录 Problem Solution Analysis Code Problem portal: 1012 The Best Rank (25 分) Solution Analysis ?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目 ?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可. ?可以创建四个数组,数组的每个元素存储某一门科目的

HDU 5131 Song Jiang&#39;s rank list(水题)

Song Jiang's rank list Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 259    Accepted Submission(s): 134 Problem Description <Shui Hu Zhuan>,also <Water Margin>was written by Shi Nai'

1012 The Best Rank (25)(25 分)

1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean ti

1012 The Best Rank (25分) vector与结构体排序

1012 The Best Rank (25分) To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, w

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结