zoukankan      html  css  js  c++  java
  • 10-排序5 PAT Judge

    用了冒泡和插入排序 果然没有什么本质区别。。都是运行超时

    用库函数sort也超时

    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 (10​^4​​), the total number of users, K (5), the total number of problems, and M 10^5​​), 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 <stdio.h>
      2 
      3 struct user
      4 {
      5     int id;
      6     int score[6];
      7     int solved;
      8     int sum;
      9     int flag;
     10 }users[10005];
     11 
     12 
     13 int main()
     14 {
     15     int N, K, M, max = 0;
     16     scanf("%d %d %d", &N, &K, &M);
     17     int p[6] = {0};
     18     for(int i = 1; i <= K; i++) {
     19         scanf("%d",&p[i]);
     20         max += p[i];
     21     } 
     22      
     23     //初始化 
     24     for(int i = 0; i <= N;i++) {
     25         users[i].id = i;
     26         users[i].solved = 0;
     27         users[i].sum = 0;
     28         users[i].flag = 0;
     29         
     30         for(int j = 0; j < 6; j++)
     31             users[i].score[j] = -2;
     32     }
     33     
     34     int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
     35     for(int i = 0; i < M; i++) {    
     36         scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
     37         if(users[tmpUserId].score[tmpProId] < tmpPSO) {
     38             if(tmpPSO > -1)
     39                 users[tmpUserId].flag = 1;                    //有提交 how about -1 
     40             users[tmpUserId].score[tmpProId] = tmpPSO;    //更新最高分 
     41 
     42             if(tmpPSO == p[tmpProId])                    //满分的问题数 
     43                 users[tmpUserId].solved++;
     44         }    
     45     }
     46     
     47     //统计sum
     48     for(int i = 1; i <= N; i++) {
     49         for(int j = 1; j < 6; j++) {
     50             if(users[i].score[j] > 0)
     51                 users[i].sum += users[i].score[j];
     52         }
     53     } 
     54 //    for(int i = 1; i <= N; i++)
     55 //        printf("%d : %d
    ",i,users[i].sum);
     56     //冒泡排序 
     57     int sort[10005];    //存放顺序 
     58     for(int i = 1; i <= N; i++)
     59         sort[i] = i;
     60     for(int i = N; i > 0; i--) {
     61         int flag = 0;
     62         for(int j = 1; j < i; j++) {
     63             if( users[sort[j]].sum < users[sort[j+1]].sum ) {
     64                 int tmp = sort[j];                //交换次序
     65                 sort[j] = sort[j+1];             
     66                 sort[j+1] = tmp;
     67                 flag = 1;
     68 //                printf("j:%d  sort[j]:%d %d
    ",j,sort[j],sort[j+1]);
     69             } else if(users[sort[j]].sum == users[sort[j+1]].sum) {    //如果总分一样 
     70                 if(users[sort[j]].solved < users[sort[j+1]].solved) {    //根据完美解决问题数排序 
     71                     int tmp = sort[j];                //交换次序
     72                 sort[j] = sort[j+1];             
     73                 sort[j+1] = tmp;
     74                     flag = 1;
     75                 } //由于是冒泡排序稳定,id小的在前面 不需要再判断 
     76             }
     77         }
     78 //        for(int i = 1; i <= N; i++)
     79 //            printf("%d ",sort[i]);
     80 //        printf("
    ");
     81         if(flag == 0) break; 
     82     } 
     83     
     84 //    for(int i = 1; i <= N; i++) {
     85 //        printf("%05d %d ", users[i].id, users[i].sum);
     86 //        for(int j = 1; j < K; j++) {
     87 //            if(users[i].score[j] == -1)
     88 //                users[i].score[j] = 0;
     89 //            if(users[i].score[j] >= 0)
     90 //                printf("%d ",users[i].score[j]);
     91 //            else printf("- ");
     92 //        }
     93 //        if(users[i].score[K] >= 0)
     94 //                printf("%d
    ",users[i].score[K]);
     95 //            else printf("-
    ");    
     96 //    }
     97 
     98     //显示输出
     99     int rank = 0;
    100     int currentSum = max;
    101     for(int i = 1; i <= N; i++) {
    102         if(users[sort[i]].flag == 0)
    103             continue;
    104             
    105         if(users[sort[i]].sum == currentSum)
    106             ;
    107         else
    108             rank = i;
    109         
    110         currentSum = users[sort[i]].sum;
    111         printf("%d %05d %d ", rank, users[sort[i]].id, currentSum);
    112         
    113         for(int j = 1; j < K; j++) {
    114             if(users[sort[i]].score[j] == -1)
    115                 users[sort[i]].score[j] = 0;
    116             if(users[sort[i]].score[j] >= 0)
    117                 printf("%d ",users[sort[i]].score[j]);
    118             else printf("- ");
    119         }
    120         if(users[sort[i]].score[K] >= 0)
    121                 printf("%d
    ",users[sort[i]].score[K]);
    122             else printf("-
    ");    
    123     }
    124     
    125 
    126     return 0;    
    127 } 
    Bubble Sort
      1 #include <stdio.h>
      2 
      3 struct user
      4 {
      5     int id;
      6     int score[6];
      7     int solved;
      8     int sum;
      9     int flag;
     10 }users[10005];
     11 
     12 
     13 int main()
     14 {
     15     int N, K, M, max = 0;
     16     scanf("%d %d %d", &N, &K, &M);
     17     int p[6] = {0};
     18     for(int i = 1; i <= K; i++) {
     19         scanf("%d",&p[i]);
     20         max += p[i];
     21     } 
     22      
     23     //初始化 
     24     for(int i = 0; i <= N;i++) {
     25         users[i].id = i;
     26         users[i].solved = 0;
     27         users[i].sum = 0;
     28         users[i].flag = 0;
     29         
     30         for(int j = 0; j < 6; j++)
     31             users[i].score[j] = -2;
     32     }
     33     
     34     int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
     35     for(int i = 0; i < M; i++) {    
     36         scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
     37         if(users[tmpUserId].score[tmpProId] < tmpPSO ) {
     38             if(tmpPSO > -1)
     39                 users[tmpUserId].flag = 1;                    //有提交  
     40             users[tmpUserId].score[tmpProId] = tmpPSO;    //更新最高分 
     41 
     42 //            if(tmpPSO == p[tmpProId])                    //满分的问题数 
     43 //                users[tmpUserId].solved++;
     44         }    
     45     }
     46     
     47     //统计sum
     48     for(int i = 1; i <= N; i++) {
     49         for(int j = 1; j < 6; j++) {
     50             if(users[i].score[j] > 0) {
     51 //                users[i].flag = 1;                    //有提交  
     52                 users[i].sum += users[i].score[j];
     53                 if(users[i].score[j] == p[j])        //满分的问题数 
     54                     users[i].solved++;
     55             }    
     56         }
     57     } 
     58 
     59     //插入排序 
     60     int sort[10005];    //存放顺序 
     61     for(int i = 1; i <= N; i++)
     62         sort[i] = i;
     63     int j;
     64     for(int i = 2; i <= N; i++) {
     65         int tmp = sort[i];    
     66         for(j = i; j > 1; j--) {
     67             if(users[sort[j]].sum > users[sort[j-1]].sum) {
     68                 sort[j] = sort[j-1];
     69                 sort[j-1] = tmp;
     70             } else if(users[sort[j]].sum == users[sort[j-1]].sum) {
     71                 if(users[sort[j]].solved > users[sort[j-1]].solved) {
     72                     sort[j] = sort[j-1];
     73                     sort[j-1] = tmp;
     74                 }    
     75             } else //由于是插入排序稳定,id小的在前面 不需要再判断 
     76                 break;    
     77         }
     78 //        for(int k = 1; k <= N; k++)
     79 //            printf("%d ",sort[k]);
     80 //        printf("
    ");
     81     } 
     82     
     83 
     84 
     85 
     86     //显示输出
     87     int rank = 0;
     88     int currentSum = max;
     89     for(int i = 1; i <= N; i++) {
     90         if(users[sort[i]].flag == 0)
     91             continue;
     92             
     93         if(users[sort[i]].sum == currentSum)
     94             ;
     95         else
     96             rank = i;
     97         
     98         currentSum = users[sort[i]].sum;
     99         printf("%d %05d %d ", rank, users[sort[i]].id, currentSum);
    100         
    101         for(int j = 1; j < K; j++) {
    102             if(users[sort[i]].score[j] == -1)
    103                 users[sort[i]].score[j] = 0;
    104             if(users[sort[i]].score[j] >= 0)
    105                 printf("%d ",users[sort[i]].score[j]);
    106             else printf("- ");
    107         }
    108         if(users[sort[i]].score[K] >= 0)
    109                 printf("%d
    ",users[sort[i]].score[K]);
    110             else printf("-
    ");    
    111     }
    112     
    113 
    114     return 0;    
    115 } 
    Insertion sort
      1 #include <iostream>
      2 #include <algorithm>
      3 #include <vector>
      4 #include <cstdio>
      5 using namespace std;
      6 
      7 struct user
      8 {
      9     int id;
     10     int score[6];
     11     int solved;
     12     int sum;
     13     int flag;
     14 }users[10005];
     15 
     16 
     17 /*cmp函数的返回值为true和false或1和0,
     18 若为true/1,则sort()函数为升序排列,
     19 若为false/0,则sort()函数为降序排列。
     20 */
     21 bool cmp(user u1, user u2) {
     22     if (u1.sum != u2.sum){
     23         return u1.sum > u2.sum;
     24     } else{
     25         if (u1.solved != u2.solved) {
     26             return u1.solved > u2.solved;
     27         } else{
     28             return u1.id < u2.id;
     29         }
     30     }
     31 }
     32 
     33 int main()
     34 {
     35     int N, K, M, max = 0;
     36     scanf("%d %d %d", &N, &K, &M);
     37     int p[6] = {0};
     38     for(int i = 1; i <= K; i++) {
     39         scanf("%d",&p[i]);
     40         max += p[i];
     41     } 
     42      
     43     //初始化 
     44     for(int i = 0; i <= N;i++) {
     45         users[i].id = i;
     46         users[i].solved = 0;
     47         users[i].sum = 0;
     48         users[i].flag = 0;
     49         
     50         for(int j = 0; j < 6; j++)
     51             users[i].score[j] = -2;
     52     }
     53     
     54     int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
     55     for(int i = 0; i < M; i++) {    
     56         scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
     57         if(users[tmpUserId].score[tmpProId] < tmpPSO ) {
     58             if(tmpPSO > -1)
     59                 users[tmpUserId].flag = 1;                    //有提交  
     60             users[tmpUserId].score[tmpProId] = tmpPSO;    //更新最高分 
     61 
     62 //            if(tmpPSO == p[tmpProId])                    //满分的问题数 
     63 //                users[tmpUserId].solved++;
     64         }    
     65     }
     66     
     67      vector<user> vec;
     68     //统计sum
     69     for(int i = 1; i <= N; i++) {
     70         for(int j = 1; j < 6; j++) {
     71             if(users[i].score[j] > 0) {
     72 //                users[i].flag = 1;                    //有提交   
     73                 users[i].sum += users[i].score[j];
     74                 if(users[i].score[j] == p[j])        //满分的问题数 
     75                     users[i].solved++;
     76             }    
     77         }
     78         if(users[i].sum >= 0)
     79             vec.push_back(users[i]);
     80     } 
     81     
     82     sort(vec.begin(),vec.end(),cmp);
     83 
     84     //显示输出
     85     int rank = 0;
     86     int currentSum = max;
     87     for(int i = 0; i < N; i++) {
     88         if(vec[i].flag == 0)
     89             continue;
     90             
     91         if(vec[i].sum == currentSum)
     92             ;
     93         else
     94             rank = i+1;
     95         
     96         currentSum = vec[i].sum;
     97         printf("%d %05d %d ", rank, vec[i].id, currentSum);
     98         
     99         for(int j = 1; j < K; j++) {
    100             if(vec[i].score[j] == -1)
    101                 vec[i].score[j] = 0;
    102             if(vec[i].score[j] >= 0)
    103                 printf("%d ",vec[i].score[j]);
    104             else printf("- ");
    105         }
    106         if(vec[i].score[K] >= 0)
    107                 printf("%d
    ",vec[i].score[K]);
    108             else printf("-
    ");    
    109     }
    110     return 0;    
    111 } 
    sort
     
  • 相关阅读:
    eclipse config 2 tab -&gt; space
    APUE读书笔记-第13章-守护进程
    【转】Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
    【转】Android学习基础自定义Checkbox组件
    【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解
    【转】android 自定义控件 使用declare-styleable进行配置属性(源码角度)
    【转】Android SwitchButton(滑动开关)
    【转】Android 如何在Eclipse中查看Android API源码 及 support包源码
    【转】如何在eclipse里关联查看android源码
    【转】setTag()/getTag()
  • 原文地址:https://www.cnblogs.com/kuotian/p/5473604.html
Copyright © 2011-2022 走看看