World Cup CodeForces - 931B(暴力,最近公共祖先)

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.

Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.

In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.

In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.

题意: n个人,相邻两个人两两对决。1和2,3和4。。。然后留下胜利的人。你可以控制每局胜负。给出a,b。问a和b在第几局相遇。如果是最后一局,输出final。
思路:
可以暴力模拟。
正解是lca。建树以后,a,b两点lca对应的高高度就是答案。
个人感觉和E. Tournament 1260E(dp,贪心)
https://blog.csdn.net/tomjobs/article/details/103339185
这道题很像。

所以可以logn模拟建树找祖先过程。找到的是祖先的高度。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

int main()
{
    int n,a,b;scanf("%d%d%d",&n,&a,&b);if(a > b)swap(a,b);
    int num = n,cnt = 0;while(num != 1){cnt++;num >>= 1;}
    n >>= 1;
    int ans = cnt;
    while(n)
    {
        if(a <= n && b > n)
        {
            break;
        }
        else if(a > n && b > n)
        {
            a -= n;
            b -= n;
        }
        else if(a <= n && b <= n)
        {
            n = n / 2;
            ans--;
        }
    }
    if(ans == cnt)
    {
        printf("Final!\n");
    }
    else printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值