码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 200C Football Championship(暴力枚举)

时间:2015-08-06 18:27:03      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   暴力   

C. Football Championship
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Any resemblance to any real championship and sport is accidental.

The Berland National team takes part in the local Football championship which now has a group stage. Let‘s describe the formal rules of the local championship:

  • the team that kicked most balls in the enemy‘s goal area wins the game;
  • the victory gives 3 point to the team, the draw gives 1 point and the defeat gives 0 points;
  • a group consists of four teams, the teams are ranked by the results of six games: each team plays exactly once with each other team;
  • the teams that get places 1 and 2 in the group stage results, go to the next stage of the championship.

In the group stage the team‘s place is defined by the total number of scored points: the more points, the higher the place is. If two or more teams have the same number of points, then the following criteria are used (the criteria are listed in the order of falling priority, starting from the most important one):

  • the difference between the total number of scored goals and the total number of missed goals in the championship: the team with a higher value gets a higher place;
  • the total number of scored goals in the championship: the team with a higher value gets a higher place;
  • the lexicographical order of the name of the teams‘ countries: the country with the lexicographically smaller name gets a higher place.

The Berland team plays in the group where the results of 5 out of 6 games are already known. To be exact, there is the last game left. There the Berand national team plays with some other team. The coach asks you to find such score X:Y (where X is the number of goals Berland scored and Y is the number of goals the opponent scored in the game), that fulfills the following conditions:

  • X > Y, that is, Berland is going to win this game;
  • after the game Berland gets the 1st or the 2nd place in the group;
  • if there are multiple variants, you should choose such score X:Y, where value X?-?Y is minimum;
  • if it is still impossible to come up with one score, you should choose the score where value Y (the number of goals Berland misses) is minimum.
Input

The input has five lines.

Each line describes a game as "team1 team2 goals1:goals2" (without the quotes), what means that team team1 played a game with team team2, besides, team1 scored goals1 goals and team2 scored goals2 goals. The names of teams team1 and team2 are non-empty strings, consisting of uppercase English letters, with length of no more than 20 characters; goals1,?goals2 are integers from 0 to 9.

The Berland team is called "BERLAND". It is guaranteed that the Berland team and one more team played exactly 2 games and the the other teams played exactly 3 games.

Output

Print the required score in the last game as X:Y, where X is the number of goals Berland scored and Y is the number of goals the opponent scored. If the Berland team does not get the first or the second place in the group, whatever this game‘s score is, then print on a single line "IMPOSSIBLE" (without the quotes).

Note, that the result score can be very huge, 10:0 for example.

Sample test(s)
input
AERLAND DERLAND 2:1
DERLAND CERLAND 0:3
CERLAND AERLAND 0:1
AERLAND BERLAND 2:0
DERLAND BERLAND 4:0
output
6:0
input
AERLAND DERLAND 2:2
DERLAND CERLAND 2:3
CERLAND AERLAND 1:3
AERLAND BERLAND 2:1
DERLAND BERLAND 4:1
output
IMPOSSIBLE
Note

In the first sample "BERLAND" plays the last game with team "CERLAND". If Berland wins with score 6:0, the results‘ table looks like that in the end:

  1. AERLAND (points: 9, the difference between scored and missed goals: 4, scored goals: 5)
  2. BERLAND (points: 3, the difference between scored and missed goals: 0, scored goals: 6)
  3. DERLAND (points: 3, the difference between scored and missed goals: 0, scored goals: 5)
  4. CERLAND (points: 3, the difference between scored and missed goals: -4, scored goals: 3)

In the second sample teams "AERLAND" and "DERLAND" have already won 7 and 4 points, respectively. The Berland team wins only 3 points, which is not enough to advance to the next championship stage.

题意描述:

四支球队两两比赛,一共比赛6场,排名前2名的队伍晋级。告诉你前5场的比赛结果,输出在最后一场比赛中BERLAND队如果想晋级的最低的符合要求的比分。
队伍排名和正式足球比赛排名方式相似:先按总得分排名,若总得分相同则按净胜球数排名,若净胜球数相同则按进球数排名,若进球数相同则按队名字典序排名(╯‵□′)╯︵┻━┻。                                                                                                                          
对于最低的符合要求的比分,题目是这样描述的:在获胜的前提下使净胜球数最小,即X:Y中的X-Y最小,若由多种情况满足条件则选择Y最小的情况。


解题思路:

每场每队的进球数在0-9之间,那么暴力枚举即可。关键的是枚举的顺序。首先我们要保证B队获胜,那么X-Y一定大于0,所以我们可以枚举B队净胜球数(即X-Y),再枚举B队失球数(即Y),每次枚举后排序,若B队在前2名则更新答案,这样两层循环之后就可以得出使得(X-Y)最小,同时Y最小的答案。
另外:除了B队外的3支队伍的队名是没有规律的,不要被样例迷惑。


参考代码:

#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=2100;
struct team
{
    string name;
    int cnt,point,get,lost;
};

bool cmp(const team& a,const team& b)//按照题目的描述进行排序
{
    if(a.point!=b.point)
        return a.point>b.point;
    if((a.get-a.lost)!=(b.get-b.lost))
        return (a.get-a.lost)>(b.get-b.lost);
    if(a.get!=b.get)
        return a.get>b.get;
    return a.name<b.name;
}

map<string,int>m;
team t[5];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    char t1[200],t2[200],temp;
    int a,b;
    while(cin>>t1>>t2>>a>>temp>>b)
    {
        m.clear();
        int flag=2;
        m[t1]=1;
        m[t2]=2;
        int cnt=4;
        t[1].name=t1;
        t[2].name=t2;
        t[m[t1]].get=t[m[t2]].lost=a;
        t[m[t1]].lost=t[m[t2]].get=b;
        t[m[t1]].cnt++;
        t[m[t2]].cnt++;
        if(a>b)
        {
            t[m[t1]].point+=3;
            t[m[t2]].point+=0;
        }
        else if(a==b)
        {
            t[m[t1]].point+=1;
            t[m[t2]].point+=1;
        }
        else
        {
            t[m[t1]].point+=0;
            t[m[t2]].point+=3;
        }
        while(cnt--)
        {
            cin>>t1>>t2>>a>>temp>>b;
            if(m[t1]==0)
                m[t1]=++flag;
            if(m[t2]==0)
                m[t2]=++flag;
            t[m[t1]].name=t1;
            t[m[t2]].name=t2;
            t[m[t1]].get+=a;
            t[m[t2]].lost+=a;
            t[m[t1]].lost+=b;
            t[m[t2]].get+=b;
            t[m[t1]].cnt++;
            t[m[t2]].cnt++;
            if(a>b)
            {
                t[m[t1]].point+=3;
                t[m[t2]].point+=0;
            }
            else if(a==b)
            {
                t[m[t1]].point+=1;
                t[m[t2]].point+=1;
            }
            else
            {
                t[m[t1]].point+=0;
                t[m[t2]].point+=3;
            }
        }
        int opponent,me;
        for(int i=1; i<=4; i++)
            if(t[i].name!="BERLAND"&&t[i].cnt==2)
                opponent=i;
            else if(t[i].name=="BERLAND")
                me=i;
        int ans1=INF,ans2=INF;
        for(int dis=50; dis>=1; dis--)//枚举(X-Y)
        {
            for(int los=50; los>=0; los--)//枚举Y
            {
                int ge=los+dis;
                team a[5];
                a[1]=t[1];
                a[2]=t[2];
                a[3]=t[3];
                a[4]=t[4];
                a[me].get+=ge;
                a[opponent].lost+=ge;
                a[me].lost+=los;
                a[opponent].get+=los;
                if(ge>los)
                {
                    a[me].point+=3;
                    a[opponent].point+=0;
                }
                else if(ge==los)
                {
                    a[me].point+=1;
                    a[opponent].point+=1;
                }
                else
                {
                    a[me].point+=0;
                    a[opponent].point+=3;
                }
                sort(a+1,a+4+1,cmp);
                if(a[1].name=="BERLAND"||a[2].name=="BERLAND")
                {
                    ans1=ge;
                    ans2=los;
                }
            }
        }
        if(ans1==INF||ans2==INF)
            printf("IMPOSSIBLE\n");
        else
            printf("%d:%d\n",ans1,ans2);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

CodeForces 200C Football Championship(暴力枚举)

标签:acm   算法   暴力   

原文地址:http://blog.csdn.net/noooooorth/article/details/47319503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!