zoukankan      html  css  js  c++  java
  • 链表,关键是结构体的快排

    金牌、银牌、铜牌

    Time Limit: 1000MS    Memory limit: 65536K

    题目描述

    Acm——大学中四大竞赛之首——是极具挑战性的大学生竞赛形式。在一场acm比赛中,一个参赛队伍由三人组合而成,在最短的时间内做出尽可能多的题目而且要尽量少提交错误代码,这样才能得到更高的排名。现在让我们模拟一次不正规的acm比赛,假设在比赛开始后30分钟(这时已经有不少同学提交了代码,在rating中已经出现),到比赛结束前,又有新的同学提交(在rating中出现),同时rating在不断变化着,还有一些同学因为一些原因中途退出比赛(这时rating中自动删除,当然在正式比赛中不会有这种情况)。最后终于比赛结束啦,看看rating,都有谁能拿到奖牌呢?

    输入

    第一行一个整数n(n<=1000),代表开始比赛后30分钟已经有n个人提交了代码。从第二行到第n+1行每行包括名字name(小于20个字符),分数p(0<=p<=10000),同时行数代表目前排名情况,第二行的排名第一,第三行排名第二,依次类推。

    从第n+2行起,每行有一个字符,是A,Q,C,S,O中的一个:

    A代表新加进rating中一名同学,紧随其后是名字name(小于20个字符),分数p(0<=p<=10000);

    Q代表有一名同学退出了,接着是他的名字name(小于20个字符);

    C代表有一个人的分数发生的改变,接着是此人的名字name,他的分数加多少(分数只会增加不会减少);

    S代表一次显示此时rating的请求,这时输出所有在rating中的同学名字及他们的分数。

    O代表比赛结束。

    输出

    对每次请求,输出此时rating中的所有同学名字和对应分数,并输出一个空行,在比赛结束时输出金牌获得者(一名),银牌获得者(两名),铜牌获得者(三名)(测试数据保证此时有至少6名同学在rating上)。如果分数相同的则并列为相同奖。更详细的输出见示例。

    示例输入

    7
    cze 90
    qch 87
    zff 70
    shangcf 66
    zhaohq 50
    zhrq 46
    yah 20
    A pc 56
    Q zff
    C qch 4
    S
    A von 66
    O
    

    示例输出

    qch 91
    cze 90
    shangcf 66
    pc 56
    zhaohq 50
    zhrq 46
    yah 20
    
    #1 : qch
    #2 : cze shangcf von
    #3 : pc zhaohq zhrq
    
    01.#include<stdio.h>  
    02.#include<string.h>  
    03.#include<algorithm>  
    04.struct node  
    05.{  
    06.    int num,score;  
    07.    char s[100];  
    08.}st[10010];  
    09.int cmp(const struct node x,const struct node y )  
    10.{  
    11.    if(x.score!=y.score)  
    12.        return x.score>y.score; 先按分数从大到小排。 
    13.    return x.num<y.num; 分数相同的情况下,名字出现在前面的排在前面。 
    14.}  
    15.int main ()  
    16.{  
    17.    int n,i,sc,ss;  
    18.    scanf("%d",&n);  
    19.    for(i=0;i<n;i++)  
    20.    {  
    21.        scanf("%s %d",st[i].s,&st[i].score);  
    22.        st[i].num=i;  
    23.    }  
    24.    int sum=n;  
    25.    char ch,name[100];  
    26.    while(~scanf("%c",&ch))  
    27.    {  
    28.        if(ch=='A')  
    29.        {  
    30.            scanf("%s %d",st[n].s,&st[n].score);  
    31.            st[n].num=n;  
    32.            n++;  
    33.            sum++;  
    34.        }  
    35.        else if(ch=='Q')  
    36.        {  
    37.            scanf("%s",name);  
    38.            for(i=0;i<n;i++)  
    39.            {  
    40.                if(strcmp(name,st[i].s)==0)  
    41.                {  
    42.                    st[i].score=-10;  
    43.                    break;  
    44.                }  
    45.            }  
    46.            sum--;  
    47.        }  
    48.        else if(ch=='C')  
    49.        {  
    50.            scanf("%s %d",name,&sc);  
    51.            for(i=0;i<n;i++)  
    52.            {  
    53.                if(strcmp(name,st[i].s)==0)  
    54.                {  
    55.                    st[i].score+=sc;  
    56.                    break;  
    57.                }  
    58.            }  
    59.        }  
    60.        else if(ch=='S')  
    61.        {  
    62.            std::sort(st,st+n,cmp);  
    63.            for(i=0;i<sum;i++)  
    64.                    printf("%s %d\n",st[i].s,st[i].score);  
    65.            printf("\n");  
    66.        }  
    67.        else if(ch=='O')  
    68.        {  
    69.            std::sort(st,st+n,cmp);  
    70.            int j=0,k;  
    71.            printf("#1 :");  
    72.            k=j;  
    73.            printf(" %s",st[j++].s);  
    74.            while(st[j].score==st[k].score)  
    75.            {  
    76.                printf(" %s",st[j].s);  
    77.                j++;  
    78.            }  
    79.             printf("\n#2 :");  
    80.             for(ss=2;ss>0&&j<sum;ss--)  
    81.             {  
    82.                 k=j;  
    83.                 printf(" %s",st[j++].s);  
    84.                 while(st[j].score==st[k].score)  
    85.                 {  
    86.                     printf(" %s",st[j++].s);  
    87.                 }  
    88.             }  
    89.             printf("\n#3 :");  
    90.             for(ss=3;ss>0&&j<sum;ss--)  
    91.             {  
    92.                 k=j;  
    93.                 printf(" %s",st[j++].s);  
    94.                 while(st[j].score==st[k].score)  
    95.                 {  
    96.                     printf(" %s",st[j++].s);  
    97.                 }  
    98.             }  
    99.             break;  
    100.        }  
    101.  
    102.  
    103.        }  
    104.        return 0;  
    105.    }  
    106.  
    107.  
    108.  
    109.  
    110.  
  • 相关阅读:
    [LeetCode] 310. Minimum Height Trees
    [LeetCode] 722. Remove Comments
    [LeetCode] 243, 244, 245. Shortest Word Distance I, II, III
    [LeetCode] 939. Minimum Area Rectangle
    [LeetCode] 135. Candy
    [LeetCode] 1395. Count Number of Teams
    [LeetCode] 673. Number of Longest Increasing Subsequence
    [LeetCode] 724. Find Pivot Index
    [LeetCode] 1219. Path with Maximum Gold
    [LeetCode] 849. Maximize Distance to Closest Person
  • 原文地址:https://www.cnblogs.com/LK1994/p/2989905.html
Copyright © 2011-2022 走看看