Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately.
Organizers are still arranging tournament grid (i.e. the order games will happen and who is going to play with whom) but they have already fixed one rule: two players can play against each other only if the number of games one of them has already played differs by no more than one from the number of games the other one has already played. Of course, both players had to win all their games in order to continue participating in the tournament.
Tournament hasn't started yet so the audience is a bit bored. Ostap decided to find out what is the maximum number of games the winner of the tournament can take part in (assuming the rule above is used). However, it is unlikely he can deal with this problem without your help.
The only line of the input contains a single integer n (2 ≤ n ≤ 1018) — the number of players to participate in the tournament.
Print the maximum number of games in which the winner of the tournament can take part.
2
1
3
2
4
2
10
4
In all samples we consider that player number 1 is the winner.
In the first sample, there would be only one game so the answer is 1.
In the second sample, player 1 can consequently beat players 2 and 3.
In the third sample, player 1 can't play with each other player as after he plays with players 2 and 3 he can't play against player 4, as he has 0 games played, while player 1 already played 2. Thus, the answer is 2 and to achieve we make pairs (1, 2) and (3, 4) and then clash the winners.
Source
Codeforces Round #382 (Div. 2)
My Solution
题意:每个人输了比赛就会被淘汰,每两个人可以打比赛的要求是a赢过x场比赛b赢过y场比赛则当abs(x - y) <= 1 时他们可以进行比赛,总共n个选手,问最终的赢家可能赢过的场次的最大值。
递推、斐波拉契数列
递推是很好写的,cost[i] 表示赢i场比赛需要花费多少人,
则 cost[0] = 1,cost[1] = 2, cost[3] = cost[1] + cost[0], cost[ans] = cost[ans-1] + cost[ans-2]一次类推直到n不够用,ans即为答案。
原来是斐波拉契数列,Y ^_^ Y
复杂度 O(85)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
LL cost[maxn];
int main()
{
#ifdef LOCAL
freopen("c.txt", "r", stdin);
//freopen("c.out", "w", stdout);
int T = 5;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
LL n, ans = 0;
cin >> n;
cost[ans] = 1;
ans++; n -= 2;
cost[ans] = 2;
while(true){
if(n >= cost[ans - 1]){
n -= cost[ans - 1];
ans++;
cost[ans] = cost[ans - 1] + cost[ans - 2];
}
else break;
}
cout << ans << endl;
#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}
Thank you!
------from ProLights