PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)

题意:

给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名。

分析:

  题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后一个测试点超时。

  发现自己一开始太傻太盲目,其实只要一次性全部输进来,记录好考场编号,一次排序就可以了。既然只排了一次,怎么计算考场排名呢,这里我用了三个数组

int g_rank[100];//记录各个考场当前排到的名次 (当前最后一个人的名次)
int g_score[100];//记录个考场当前排到的最后一个人的分数
int g_num[100];//记录个考场当前已经排好队的人数  

帮助计算考场排名

        //计算分组排名
        int p=a[i].num;//他属于第p组
        if(g_rank[p]==0){//如果他是本小组第一名
            a[i].rank=1;
            g_rank[p]=1;//当前组排到了第几名
            g_score[p]=a[i].score;//本小组目前排下来最后一名的分数
            g_num[p]=1;//当前组排到了第几个人
        }else{
            g_num[p]++;//更新人数
            if(g_score[p]==a[i].score) {//如果此人与本组的上一人的分数相同
                a[i].rank=g_rank[p];//就是本小组上一名的名次
            }else{//不同的话
                a[i].rank=g_num[p];//那么他的名次是本组的人数
                g_rank[p]=g_num[p];//更新当前本组最后一人名次
                g_score[p]=a[i].score;//更新分数
            }
        }

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
    string name;
    int score;
    int num;
    int rank;
    int frank;
}a[30005];
bool cmp(node x,node y){
    if(x.score==y.score){
        return x.name<y.name;
    }else{
        return x.score>y.score;
    }
}
int g_rank[100];//记录各个考场当前排到的名次 (当前最后一个人的名次)
int g_score[100];//记录个考场当前排到的最后一个人的分数
int g_num[100];//记录个考场当前已经排好队的人数
int main(){
    int n,k;
    cin>>n;
    int r=1;
    for(int i=1;i<=n;i++){
        cin>>k;
        for(int j=1;j<=k;j++){
            cin>>a[r].name>>a[r].score;
            a[r].num=i;//考场编号
            r++;
        }
    }
    sort(a+1,a+r,cmp);
    cout<<r-1<<endl;
    memset(g_rank,0,sizeof(g_rank));
    for(int i=1;i<r;i++){
        //全部排名
        if(i==1){
            a[i].frank=1;
        }else{
                if(a[i].score==a[i-1].score){
                    a[i].frank=a[i-1].frank;
                }else{
                    a[i].frank=i;
                }
        }
        //计算分组排名
        int p=a[i].num;//他属于第p组
        if(g_rank[p]==0){//如果他是本小组第一名
            a[i].rank=1;
            g_rank[p]=1;//当前组排到了第几名
            g_score[p]=a[i].score;//本小组目前排下来最后一名的分数
            g_num[p]=1;//当前组排到了第几个人
        }else{
            g_num[p]++;//更新人数
            if(g_score[p]==a[i].score) {//如果此人与本组的上一人的分数相同
                a[i].rank=g_rank[p];//就是本小组上一名的名次
            }else{//不同的话
                a[i].rank=g_num[p];//那么他的名次是本组的人数
                g_rank[p]=g_num[p];//更新当前本组最后一人名次
                g_score[p]=a[i].score;//更新分数
            }
        }
    }
    for(int i=1;i<r;i++){
        cout<<a[i].name<<" "<<a[i].frank<<" "<<a[i].num<<" "<<a[i].rank<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caiyishuai/p/11361102.html

时间: 2024-07-30 19:09:40

PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)的相关文章

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

【PAT甲级】1032 Sharing (25分)

1032 Sharing (25分) To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored a

【PAT甲级】1003 Emergency (25分)

1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between an

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The