zoukankan      html  css  js  c++  java
  • A1075. PAT Judge

    The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 3 positive integers, N (<=104), the total number of users, K (<=5), the total number of problems, and M (<=105), the total number of submittions. It is then assumed that the user id's are 5-digit numbers from 00001 to N, and the problem id's are from 1 to K. The next line contains K positive integers p[i] (i=1, ..., K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submittion in the following format:

    user_id problem_id partial_score_obtained

    where partial_score_obtained is either -1 if the submittion cannot even pass the compiler, or is an integer in the range [0, p[problem_id]]. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, you are supposed to output the ranklist in the following format:

    rank user_id total_score s[1] ... s[K]

    where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

    The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

    Sample Input:

    7 4 20
    20 25 25 30
    00002 2 12
    00007 4 17
    00005 1 19
    00007 2 25
    00005 1 20
    00002 2 2
    00005 1 15
    00001 1 18
    00004 3 25
    00002 2 25
    00005 3 22
    00006 4 -1
    00001 2 18
    00002 1 20
    00004 1 15
    00002 4 18
    00001 3 4
    00001 4 2
    00005 2 -1
    00004 2 0
    

    Sample Output:

    1 00002 63 20 25 - 18
    2 00005 42 20 0 22 -
    2 00007 42 - 25 - 17
    2 00001 42 18 18 4 2
    5 00004 40 15 0 25 -

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 using namespace std;
     6 typedef struct infoStruct{
     7     int id;
     8     int prob[6];
     9     int perfect;
    10     int full;
    11     int rank;
    12     int shown;
    13     infoStruct(){
    14         perfect = 0;
    15         shown = 0;
    16         for(int i = 0; i < 6; i++)
    17             prob[i] = -2;
    18     }
    19 }info;
    20 bool cmp(info a, info b){
    21     if(a.full != b.full)
    22         return a.full > b.full;
    23     else if(a.perfect != b.perfect)
    24         return a.perfect > b.perfect;
    25     else return a.id < b.id;
    26 }
    27 info user[10001];
    28 int main(){
    29     int N, K, M, fullSco[6];
    30     int tempPro, tempSco, id;
    31     scanf("%d%d%d", &N, &K, &M);
    32     for(int i = 1; i <= K; i++)
    33         scanf("%d", &fullSco[i]);
    34     for(int i = 0; i <= N; i++)
    35         user[i].id = i;
    36     for(int i = 0; i < M; i++){
    37         scanf("%d%d%d", &id, &tempPro, &tempSco);
    38         if(tempSco == fullSco[tempPro] && user[id].prob[tempPro] != fullSco[tempPro])
    39             user[id].perfect++;
    40         if(user[id].prob[tempPro] < tempSco){
    41             user[id].prob[tempPro] = tempSco;
    42             if(tempSco >= 0)
    43                 user[id].shown = 1;
    44         }
    45     }
    46     for(int i = 1; i <= N; i++){
    47         int ans = 0;
    48         for(int j = 1; j <= K; j++){
    49             if(user[i].prob[j] > 0)
    50                 ans += user[i].prob[j];
    51         }
    52         user[i].full = ans;
    53     }
    54     sort(user + 1, user + (N + 1), cmp);
    55     user[1].rank = 1;
    56     for(int i = 2; i <= N; i++){
    57         if(user[i].full == user[i - 1].full)
    58             user[i].rank = user[i - 1].rank;
    59         else user[i].rank = i;
    60     }
    61     for(int i = 1; i <= N; i++){
    62         if(user[i].shown == 1){
    63             printf("%d %05d %d", user[i].rank, user[i].id, user[i].full);
    64             for(int j = 1; j <= K; j++){
    65                 if(user[i].prob[j] == -1)
    66                     printf(" 0");
    67                 else if(user[i].prob[j] == -2)
    68                     printf(" -");
    69                 else printf(" %d", user[i].prob[j]);
    70             }
    71             printf("
    ");
    72         }
    73     }
    74     cin >> K;
    75     return 0;
    76 }
    View Code

    总结:

    1、这道题目比较麻烦,首先输入的数据是每个考生的全部提交信息,有可能一道题交多次,也可能一次也没交。最终要求统计输出每个考生的信息,按照总分排名顺序。这里有几个注意的地方。1)N个考生,他们的id是从1到N连续的,所以可以用int存储id,并且在输入时将id作为数组的下标,这样便于将同一个人的多次提交全部汇总。 2)未编译通过的题目(为-1)在计算总分时按0分算,在最终输出时也输出0。 3)全部未提交或者全部未编译成功的考生不做输出,但全为0分的考生需要输出。所以需要区分未提交、未编译通过、提交且编译通过但得分为0这三种情况。

    2、struct的初始化

      typedef struct infoStruct{
        int id;
        int prob[6];
        int perfect;
        int full;
        int rank;
        int shown;
        infoStruct(){
          perfect = 0;
          shown = 0;
          for(int i = 0; i < 6; i++)
          prob[i] = -2;
        }
      }info;

    3、起初写这道题时没有注意到题目里的id是从1到N连续的这一条件,盲目把struct里的id设置成了char数组型,导致每一条输入都需要从头开始找相同的id并作合并,复杂度变成了n^2,最后一个测试点超时。改为用int存储id,并将id映射为数组下标后,测试通过。今后在输入数据量较大的时候一定要先考虑到复杂度。算法超时的代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 using namespace std;
     6 typedef struct infoStruct{
     7     char id[6];
     8     int prob[6];
     9     int perfect;
    10     int full;
    11     int rank;
    12     int shown;
    13     infoStruct(){
    14         perfect = 0;
    15         shown = 0;
    16         for(int i = 0; i < 6; i++)
    17             prob[i] = -2;
    18     }
    19 }info;
    20 bool cmp(info a, info b){
    21     if(a.full != b.full)
    22         return a.full > b.full;
    23     else if(a.perfect != b.perfect)
    24         return a.perfect > b.perfect;
    25     else return strcmp(a.id, b.id) < 0;
    26 }
    27 info user[10001];
    28 int main(){
    29     int N, K, M, fullSco[6];
    30     char tempId[6];
    31     int tempPro, tempSco;
    32     scanf("%d%d%d", &N, &K, &M);
    33     for(int i = 1; i <= K; i++)
    34         scanf("%d", &fullSco[i]);
    35     for(int i = 0, j = 0; i < M; i++){
    36         scanf("%s %d%d", tempId, &tempPro, &tempSco);
    37         int index = -1;
    38         for(int k = 0; k < j; k++){
    39             if(strcmp(tempId, user[k].id) == 0){
    40                 index = k;
    41                 break;
    42             }
    43         }
    44         if(index == -1){
    45             index = j;
    46             strcpy(user[index].id, tempId);
    47             j++;
    48         }
    49         if(tempSco == fullSco[tempPro] && user[index].prob[tempPro] != fullSco[tempPro])
    50             user[index].perfect++;
    51         if(user[index].prob[tempPro] < tempSco){
    52             user[index].prob[tempPro] = tempSco;
    53             if(tempSco >= 0)
    54                 user[index].shown = 1;
    55         }
    56     }
    57     for(int i = 0; i < N; i++){
    58         int ans = 0;
    59         for(int j = 1; j <= K; j++){
    60             if(user[i].prob[j] > 0)
    61                 ans += user[i].prob[j];
    62         }
    63         user[i].full = ans;
    64     }
    65     sort(user, user + N, cmp);
    66     user[0].rank = 1;
    67     for(int i = 1; i < N; i++){
    68         if(user[i].full == user[i - 1].full)
    69             user[i].rank = user[i - 1].rank;
    70         else user[i].rank = i + 1;
    71     }
    72     for(int i = 0; i < N; i++){
    73         if(user[i].shown == 1){
    74             printf("%d %s %d", user[i].rank, user[i].id, user[i].full);
    75             for(int j = 1; j <= K; j++){
    76                 if(user[i].prob[j] == -1)
    77                     printf(" 0");
    78                 else if(user[i].prob[j] == -2)
    79                     printf(" -");
    80                 else printf(" %d", user[i].prob[j]);
    81             }
    82             printf("
    ");
    83         }
    84     }
    85     cin >> K;
    86     return 0;
    87 } 
    View Code
  • 相关阅读:
    JS小技巧
    创建 SpringBoot 项目一直 reading pom.xml
    idea无法创建springboot/springcloud项目的问题
    Spring Data Jpa执行流程分析
    JPA插入时出现(save the transient before flushing) 解决办法
    Spring Data Jpa多表动态查询
    SpringDataJpa在一对多关系映射时出现StackOverflowError
    spring-data-jpa报错org.hibernate.LazyInitializationException
    Jpa配置
    swiper轮播插件--动态修改属性值
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8492756.html
Copyright © 2011-2022 走看看