Tournament Seeding
InterviewTime limit1sMemory limit1024 MB
Given 2^n player ratings and a threshold k, find the maximum number of close games in a seeded single-elimination bracket where higher ratings always win.
Problem
You are seeding a single-elimination tournament for a one-on-one game. The number of registered players is exactly a power of , and the tournament has exactly enough rounds to decide a winner. Each player has a unique numeric rating, and when two players meet, the player with the higher rating always wins. You want the tournament to be as exciting as possible, so it must satisfy the following properties.
- The two highest rated players appear in the final, the top four appear in the semifinal, the top eight appear in the quarterfinal, and so on. The highest rated games come last.
- Subject to the above, as many games as possible are "close." A game is "close" if the difference between the two players' ratings is less than or equal to a threshold.
Given the number of rounds, the threshold, and the ratings, find the maximum number of "close" games possible under these constraints.
Input
The first line contains two integers () and (), where is the number of rounds and is the rating difference that makes a game "close."
Each of the next lines contains a single integer (), the rating of one player. All ratings are distinct.
Output
Print a single integer: the maximum number of "close" games possible in a tournament among these players that satisfies the constraints above.