poj 1030

题目

Rating
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1825 Accepted: 434

Description

One of the participants of both regional contests which took place in St. Petersburg decided to determine overall rating for all teams that took part in at least one contest. 
This participant assigned each team a unique team identifier, which was an integer from 1 to 100 inclusively. For each contest team identifiers of the participating teams were written in a column according to their place in that contest. Identifiers of the teams that had equal results were written on the same line. The participant started with the team(s) that was(were) the best in that contest (writing them on the first line) and continued in the order of decreasing results. 
Definition: Let's say that the team has place K in the contest if exactly K-1 teams performed in that contest better. 
Consider the following examples of two contests' results: 
<td width="347" colspan="2" valign="top"

Contest no. 1

Contest no. 2

place

team's id

place

team's id

1

9

1

扫描二维码关注公众号,回复: 9472326 查看本文章

3

2

7 1 4

2

5

5

5

3

1 10

6

15 8

5

6

8

31 18

6

9

10

17

7

19

8

4 20

10

21


The overall rating for the teams which took part in both contests is defined in the following way: 
1) If some team performed better in both contests than some other team (or better in one contest and with the same result in the other contest) then the overall rating of the former team is higher than the rating of the latter team. 
2) If one of the teams in question performed better in one contest and the other team performed better in another contest then their overall rating depends on the difference of their places in both contests. So, in our example team 1 is better than team 5 in the first contest with a difference of 3 places and worse in the second contest with a difference of only 1 place, therefore the overall rating of team 1 is higher than team 5's one. If the difference of the places is the same for both contests then that teams have the equal overall ratings. The latter is also true for the teams that performed equally in both contests. 
In our example only teams 1, 4, 5, and 9 participated twice. Team 1 has the highest rating, teams 5 and 9 with the equal rating follow, and team 4 has the lowest rating. 
For the teams that participated in one contest only the overall rating and their position in the resulting list cannot be always determined. They are included in the overall list (where the teams which participated twice already placed according to the rules above) if one of the following takes place: 
A) If there is a team that participated in both contests and shared the place in one of the contests with the team in question then the latter team shares the overall rating with this team too (if there is more than one such team, then they all should have the same overall rating, otherwise the overall rating of the team in question cannot be determined). 
B) If there is a position in the overall list (either at the beginning of the list, at the end of the list, or between some lines), such that before this position only the teams are located which performed better in the same contest as the team in question and after this position only the teams are located which performed worse in the same contest as the team in question, then the team in question occupies this position in the overall list. If more than one team claim to have the same position in the overall list, then their mutual order is defined by their places in their contests (look at the example below for details). 

Teams that participated in both contests

Teams that participated in one contest only

3

1

10

9 5

19

4

20

15 8

31 18

17 21


?Team 3 will occupy the first place in the overall list (rule B). 
?The positions of teams 6 and 7 cannot be determined. 
?Team 10 will share the overall rating with team 1 (rule A). 
?Team 20 will share the overall rating with team 4 (rule A). 
?Team 19 will occupy the position between teams 9, 5 and team 4 (rule B). 
?Teams 8, 15, 17, 18, 21, and 31 will finish the overall list (rule B). But the first of them will be teams 15 and 8 (that took 6th place) followed by teams 31 and 18 (that took 8th place) and teams 17 and 21 (that took 10th place). 
Your task is to write a program that will create the overall rating list using the result tables of two contests and the given rules. 

Input

The input file contains a description of the two contests, which are separated by an empty line. Each description starts with a line containing the single integer N (1 <= N <= 100) that indicates how many lines of the contest result table follow. Each line of the contest result table consists of one or more team identifiers separated by spaces. 
Every team identifier occurs at most once in the description of each contest. 

Output

Write to the output file one or more lines with the team identifiers (separated by spaces) that represent the overall rating list. The teams that share the same rating (thus written on the same line) is written in ascending order. The teams for which the overall rating is not determined should be absent in the output file.

Sample Input

6
9
7 1 4
5
15 8
31 18
17
 
  
8
3
5
1 10
6
9
19
4 20
21

Sample Output

3
1 10
5 9
19
4 20
8 15
18 31
17 21

题意分析

有100个队伍,两轮比赛,有的队伍参加了一轮有的参加了两轮,还有的没有参加。
给你两轮比赛的排名,求综合的排名

代码

Source Code

Problem: 1030   User: PaladinDu
Memory: 168K   Time: 16MS
Language: C++   Result: Accepted
  • Source Code
    #include<stdio.h>
    #include<stdlib.h>
    /*
     * 1.先是有两个比赛名次的队伍进行排名。
     * 2.踢掉一些只参加过一次的队伍。
     *   2-1.踢掉参加过的比赛中同名次队伍中,存在最终名次有不一样的队伍。
     *   2-2.踢掉参加过的比赛中存在自己前面队伍不比自己后面的队伍强的(包括最终名次相同)。
     * 3.排序有相同名次的。直接等价于参加过的比赛中同名次队伍。
     * 4.排序没有相同名次的。
     *   4-1.这里的队伍总是不和参加过两次的队伍同一名次
     *   4-2.在不同场次的队伍以自己参加过的比赛的排名做值
     *   4-3.还一样的以id做值
    */
    
    #define __INT_TEAM_NUMBER__ (100)
    unsigned char buf[1000]={0};
    typedef struct SContest
    {
        unsigned char *ratings;
        unsigned char *teams;
        unsigned char teamnumber;
    }SContest;
    typedef struct STotalRating
    {
        unsigned char *rating;
        unsigned char *team;
        unsigned char teamnumber;
    }STotalRating;
    void fReadOneRank(SContest* p_contest_info)
    {
        char ca_line[1024];
        unsigned char ratings = 0;
    
        unsigned char i=1;
        unsigned char place = 1;
        unsigned char rating =1;
        scanf("%d",&ratings);
        p_contest_info->teamnumber = 0;
        for (i=1;i<=ratings;++i)
        {
    
            char* p_buf = ca_line;
            unsigned char i_value = 0;
            gets(ca_line);
            if (ca_line[0]=='\0')
            {
                --i;
                continue;
            }
            while(*p_buf!='\0'&&*p_buf!='\n')
            {
                switch (*p_buf) {
                case '9':case '8':case '7':
                case '6':case '5':case '4':
                case '3':case '2':case '1':
                case '0':
                    i_value =i_value *10 + *p_buf-'0';
                    break;
                default:
                    if(i_value != 0)
                    {
                        p_contest_info->teams[p_contest_info->teamnumber++]=i_value;
                        p_contest_info->ratings[i_value]=rating;
                        i_value = 0;
                        ++place;
                    }
                    break;
                }
                ++p_buf;
            }
            if(i_value != 0)
            {
                p_contest_info->teams[p_contest_info->teamnumber++]=i_value;
                p_contest_info->ratings[i_value]=rating;
                i_value = 0;
                ++place;
            }
            rating = place;
        }
    }
    STotalRating s_total;
    int fCmp(const void* a,const void* b)
    {
        return s_total.rating[*((unsigned char*)a)] - s_total.rating[*((unsigned char*)b)];
    }
    SContest s_contest[2];
    int fCmp2(const void* a,const void* b)
    {
        unsigned char teama = *((unsigned char*)a);
        unsigned char teamb = *((unsigned char*)b);
        unsigned char valuea=0;
        unsigned char valueb=0;
        if(s_total.rating[teama]!=s_total.rating[teamb])
        {
            valuea= s_total.rating[teama];
            valueb=s_total.rating[teamb];
        }else if(s_total.rating[teama]%2==1//如果是偶数表示是确定的
                &&s_contest[0].ratings[teama]+s_contest[1].ratings[teama]
                !=s_contest[0].ratings[teamb]+s_contest[1].ratings[teamb])
        {
            valuea=s_contest[0].ratings[teama]+s_contest[1].ratings[teama];
            valueb=s_contest[0].ratings[teamb]+s_contest[1].ratings[teamb];
        }else
        {
            valuea=teama;
            valueb=teamb;
        }
        return valuea-valueb;
    }
    
    /*
     * @brief 清除掉无效的team,并做初步的赋值
    */
    void fClearContest(SContest* p_contest_info)
    {
        char value=0;
        unsigned char i_start = 0;
        STotalRating* p_total = &s_total;
        unsigned char value_max[__INT_TEAM_NUMBER__+1]={0};//value_max[n]表示n以前的总值得最大值
        unsigned char value_min[__INT_TEAM_NUMBER__+1]={0};//value_min[n]表示n以后的总值得最小值
    
        value_max[0]=s_total.rating[p_contest_info->teams[0]];
        for(unsigned char i=1;i<p_contest_info->teamnumber;++i)
        {
            if(s_total.rating[p_contest_info->teams[i]]
                    >value_max[i-1])
            {
                value_max[i]=s_total.rating[p_contest_info->teams[i]];
            }else
            {
                value_max[i]=value_max[i-1];
            }
        }
        value_min[p_contest_info->teamnumber-1]=s_total.rating[p_contest_info->teams[p_contest_info->teamnumber-1]];
        for( int i =p_contest_info->teamnumber-2;i>=0;--i )
        {
            if (value_min[i+1]==0||
                    (s_total.rating[p_contest_info->teams[i]]!=0
                     &&s_total.rating[p_contest_info->teams[i]]
                     < value_min[i+1]))
            {
                value_min[i] = s_total.rating[p_contest_info->teams[i]];
            }else
            {
                value_min[i] = value_min[i+1];
            }
        }
    
    
        for(unsigned char i=0;i<=p_contest_info->teamnumber;++i)
        {
    
            if(i==p_contest_info->teamnumber
                    ||p_contest_info->ratings[p_contest_info->teams[i]]
                    != p_contest_info->ratings[p_contest_info->teams[i-1]])
            {
                for(unsigned char j=i_start;j<i;++j)
                {
                    if(value!=0)
                    {
                        if(s_total.rating[p_contest_info->teams[j]]
                                ==0)
                        {//只参加过一场
                            if(value != -1)
                            {
                                s_total.rating[((int)p_contest_info->teams[j])]=value;
                            }else
                            {
                                p_contest_info->ratings[((int)p_contest_info->teams[j])]=0;
                            }
                        }
                    }else
                    {//这个rating没有两场都参加的,看下是否冲突
                        if(value_min[i_start]<=value_max[i_start]
                                &&value_min[i_start]!=0)
                        {//冲突了
                            p_contest_info->ratings[p_contest_info->teams[j]]=0;
                        }else
                        {
                            if(value_max[j]!=0)
                            {
                                s_total.rating[p_contest_info->teams[j]]=value_max[j]+1;
                            }else if(value_min[j]!=0)
                            {
                                s_total.rating[p_contest_info->teams[j]]=value_min[j]-1;
                            }else
                            {
                                s_total.rating[p_contest_info->teams[j]]=1;
                            }
    
                        }
                    }
                }
                value = 0;
                i_start = i;
            }else
            {
                if (value != 0
                    &&s_total.rating[p_contest_info->teams[i]]!=0
                    && value != s_total.rating[p_contest_info->teams[i]])
                {
                    value = -1;//表示这个是冲突的
                }
            }
            if(value == 0)
            {
                value = s_total.rating[p_contest_info->teams[i]];
            }
        }
    
    
    }
    void fShowTotal()
    {
        return;
        STotalRating* p = &s_total;
        for(int i = 0; i< s_total.teamnumber;++i)
        {
            printf("%d:%d ",s_total.team[i],s_total.rating[s_total.team[i]]);
        }
        printf("\n");
    }
    void fInit()
    {
        unsigned char* p_buf = buf;
        s_contest[0].ratings = p_buf;
        p_buf+=__INT_TEAM_NUMBER__+2;
        s_contest[0].teams = p_buf;
        s_contest[0].teamnumber = 0;
        p_buf+=__INT_TEAM_NUMBER__+2;
        s_contest[1].ratings = p_buf;
        p_buf+=__INT_TEAM_NUMBER__+2;
        s_contest[1].teams = p_buf;
        s_contest[1].teamnumber = 0;
    
        p_buf+=__INT_TEAM_NUMBER__+2;
        s_total.rating = p_buf;
        p_buf+=__INT_TEAM_NUMBER__+2;
        s_total.team = p_buf;
        s_total.teamnumber =0;
    
    }
    int main() {
    
        SContest* p_contest=s_contest;
        fInit();
        fReadOneRank(p_contest);
        fReadOneRank(p_contest+1);
        for(unsigned char i=0;i<p_contest->teamnumber;++i)
        {
            if (s_contest[1].ratings[p_contest->teams[i]] != 0)
            {//两个比赛都参加了
                s_total.team[s_total.teamnumber++] = p_contest->teams[i];
                s_total.rating[p_contest->teams[i]] = s_contest[0].ratings[p_contest->teams[i]]
                        +s_contest[1].ratings[p_contest->teams[i]];
            }
        }
        fShowTotal();
        qsort(s_total.team,s_total.teamnumber,sizeof(s_total.team[0]),fCmp);
        fShowTotal();
        unsigned char j = 0;
        for(unsigned char i = 0;i<s_total.teamnumber;++i)
        {
            unsigned char j1= j;
            if(i<s_total.teamnumber-1
            &&s_total.rating[s_total.team[i]]
               != s_total.rating[s_total.team[i+1]])
            {
                ++j;
            }
            s_total.rating[s_total.team[i]]=j1*2+2;
        }
        fShowTotal();
        fClearContest(p_contest);
        fClearContest(p_contest+1);
        unsigned char use[__INT_TEAM_NUMBER__+1]={0};
        s_total.teamnumber=0;
        for(unsigned char i =0;i<p_contest->teamnumber;++i)
        {
            if(p_contest->ratings[p_contest->teams[i]]!=0&&
                    use[p_contest->teams[i]]==0)
            {
                use[p_contest->teams[i]]=1;
                s_total.team[s_total.teamnumber++]=p_contest->teams[i];
            }
        }
        ++p_contest;
        for(unsigned char i =0;i<p_contest->teamnumber;++i)
        {
            if(p_contest->ratings[p_contest->teams[i]]!=0&&
                use[p_contest->teams[i]]==0)
            {
                use[p_contest->teams[i]]=1;
                s_total.team[s_total.teamnumber++]=p_contest->teams[i];
            }
        }
        fShowTotal();
        qsort(s_total.team,s_total.teamnumber,sizeof(s_total.team[0]),fCmp2);
        fShowTotal();
        for(unsigned char i =0;i<s_total.teamnumber;++i)
        {
            printf("%d",s_total.team[i]);
            if(i<s_total.teamnumber-1)
            {
                if(s_total.rating[s_total.team[i]]
                  !=s_total.rating[s_total.team[i+1]]
                ||(s_total.rating[s_total.team[i]]%2==1&&
                   (s_contest[0].ratings[s_total.team[i]]+s_contest[1].ratings[s_total.team[i]])
                   !=(s_contest[0].ratings[s_total.team[i+1]]+s_contest[1].ratings[s_total.team[i+1]])
                   ))
                {
                    printf("\n");
                }else
                {
                    printf(" ");
                }
            }else
            {
                printf("\n");
            }
        }
    }


发布了54 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u011255131/article/details/54646972