查找联赛中排名第 K 的球队可能获得的最高分
联赛中有N支队伍,从1到N编号。给定一个整数K ,任务是找出一个在第 K位完成的球队可以得分的最高分。
假设球队获胜得2分,失败得0分。
注意:联赛也称为循环赛,每支球队与其他球队只交手一次。
例子:
Input: N = 2, K = 2
Output: 0
Explanation: Total number of matches = 2C2 = 1
Maximum possible win for team at 2nd position = 0
The final score for team having rank 2 = 2 * 0 = 0 Points
Input: N = 5, K =3
Output: 6
Explanation: Total number of matches = nC2 = 10
Maximum possible win for team at 3rd position = 3
The final score for team 3 = 2 * = 6
方法:问题的解决基于以下观察:
- Suppose team i finishes at ith i.e team1 finishes at 1st position, team 2 finishes at 2nd position and so on. Also the score of team i will be greater than or equal to j for all i <= j.
- For K = N, If teamN (the team which finishes at last position) wins X rounds, then all other teams must wins at least X rounds. It is also known that total number of rounds in tournament is NC2 . Now calculate the maximum value of X:
- As X Win will be less than or equal to total number of wins of all teams
X <= NC2 /N = (N-1)/2
So the maximum value of X is (N- 1) / 2 . Here X denotes the number of wins by team N against first (N -1) teams. - Now team K will win at most (N – K) rounds against last (N – K) teams and (K – 1) / 2 rounds against first (K -1 ) teams. The total maximum win by team k will be summation of win against first (K -1) and last (N – K) teams. i.e
N – K + (K-1)/2 = (2*N – K – 1)/2 matches.
按照下面提到的步骤来实现上述观察:
- 对于给定的N和K值,在第 K位完成的球队的最大可能获胜次数为(2*N – K -1)/2 。
- 现在通过将计算出的最大可能获胜乘以2来计算最终得分。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate
// maximum points scored by the
// team finishing at kth position
int maxScore(int N, int K)
{
// Calculate Maximum wins
// using formula
int maxWins = (2 * N - K - 1) / 2;
// Multiply max wins by 2
// to get total points
int totalPoints = maxWins * 2;
return totalPoints;
}
// Driver code
int main()
{
int N = 5;
int k = 3;
cout << maxScore(N, k);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to calculate
// maximum points scored by the
// team finishing at kth position
public static int maxScore(int N, int K)
{
// Calculate Maximum wins
// using formula
int maxWins = (2 * N - K - 1) / 2;
// Multiply max wins by 2
// to get total points
int totalPoints = maxWins * 2;
return totalPoints;
}
// Driver code
public static void main(String[] args)
{
int N = 5;
int k = 3;
System.out.print(maxScore(N, k));
}
}
// This code is contributed by Taranpreet
Python3
# Python code for the above approach
# Function to calculate
# maximum points scored by the
# team finishing at kth position
def maxScore(N, K):
# Calculate Maximum wins
# using formula
maxWins = (2 * N - K - 1) / 2;
# Multiply max wins by 2
# to get total points
totalPoints = maxWins * 2;
return int(totalPoints)
# Driver code
N = 5;
k = 3;
print(maxScore(N, k));
# This code is contributed by Saurabh jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to calculate
// maximum points scored by the
// team finishing at kth position
static int maxScore(int N, int K)
{
// Calculate Maximum wins
// using formula
int maxWins = (2 * N - K - 1) / 2;
// Multiply max wins by 2
// to get total points
int totalPoints = maxWins * 2;
return totalPoints;
}
// Driver code
public static void Main()
{
int N = 5;
int k = 3;
Console.Write(maxScore(N, k));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
6
时间复杂度: O(1)
辅助空间: O(1)