Codeforces Round #468 (Div. 2)B-World Cup(Java)

6 篇文章 0 订阅

The last stage of Football World Cup is played using the play-off system.

There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.

Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.

Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.

It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.

Output
In the only line print “Final!” (without quotes), if teams a and b can meet in the Final.

Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.

Sample

input:4 1 2
ouput:1

题目大意:在“world cup”足球比赛中,遵循两两进行比赛(按编号)的规则,给出两只球队的编号,假设这两只球队常胜,问他们会在第几轮相遇,如果是决赛,输出“Final!”。

看了一下的头上的几位….就我一个用队列解。利用队列“先进先出”的特性,直接模拟比赛过程,胜出队伍再次入队,直到给出的两支队伍相遇为止。为了抢最快运行速度,我还用了bufferedReader(抢到了嘿嘿),要注意的是,题目给出的两支队伍编号顺序不是按从大到小的,如果是大-小这样的顺序,需要交换数值。或者把普通链队列改成优先队列,可以免于交换。求round那里写得有点乱….因为我一开始看错题以为求的是比赛数,最后测试的时候才发现是round,临时补了一个。

AC代码:

public class Main {
    public static void main(String[] args) {
        // Scanner reader = new Scanner(System.in);
        InputReader reader = new InputReader();
        Queue<Integer> teams = new LinkedList<>();
        int n = reader.nextInt();
        int a = reader.nextInt();
        int b = reader.nextInt();
        if (a > b) {
            int temp = a;
            a = b;
            b = temp;
        }
        for (int i = 1; i <= n; i++) {
            teams.add(i);
        }
        int count = 0;
        int num = n / 2;
        while (!teams.isEmpty()) {
            int teamA = teams.poll();
            int teamB = teams.poll();
            count++;
            // System.out.println("第" + count + "局 " + teamA + " vs " + teamB);
            if (teamA == a && teamB == b) {
                if (count == n - 1) {
                    System.out.println("Final!");
                    break;
                }
                int round = 1;
                int num2 = num;
                while (count > num) {
                    round++;
                    num2 /= 2;
                    num += num2;
                }
                System.out.println(round);
                break;
            }
            if (teamA == a || teamA == b) {
                teams.add(teamA);
                // System.out.println("胜出是:"+teamA);
                continue;
            }
            if (teamB == a || teamB == b) {
                teams.add(teamB);
                // System.out.println("胜出是:"+teamB);
                continue;
            }
            teams.add(teamA);
            // System.out.println("胜出是:"+teamA);
        }
    }
}

class InputReader {
    BufferedReader buf;
    StringTokenizer tok;

    InputReader() {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }

    boolean hasNext() {
        while (tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buf.readLine());
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    String next() {
        if (hasNext())
            return tok.nextToken();
        return null;
    }

    char nextChar() {
        return next().charAt(0);
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    /*
     * BigInteger nextBigInteger() { return new BigInteger(next()); }
     * 
     * BigDecimal nextBigDecimal() { return new BigDecimal(next()); }
     */
}

排名

纪念第一次运行速度排第一梯队。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值