📜  编码比赛的最大可能评分

📅  最后修改于: 2021-09-07 02:32:43             🧑  作者: Mango

给定两个正整数数组Point[] ,大小为N 的Upvote[]和值K (1 <= K <= N)。任务是选择至少 K 个元素(问题),使得编码竞赛的评分最大。比赛评分:比赛评分定义为比赛中所有问题的总分乘以比赛中所有问题的最小赞成票

例子:

方法 :

  • 尝试每个upvote的值从高到低,同时保持尽可能大的积分组,不断给总分加分,如果比赛中的问题数量超过K,则淘汰得分最低的问题。这包括三个步骤。
    1. 按问题的投票值降序排列问题。
    2. 对于索引 i = 0, 1, …, K-1,我们将点推入 min_heap 并计算评级。我们只需要记录最大评级。我们使用 min_heap 来跟踪最小点的问题。
    3. 对于索引 i = K, K+1, …, N-1,如果当前问题的点大于 min_heap 的顶部,我们弹出现有元素并将当前元素推入 min_heap 并更新最大值评分。
      以这种方式,计算关于第 i 个最大赞成票的问题的最大评分,因为我们在 min_heap 中有 K 个最大点的问题。

下面是上述方法的实现。

C++
// C++ program to find the Maximum 
// Possible Rating of a Coding Contest
#include 
using namespace std;
  
// Function to sort all problems
// descending to upvotes
bool Comparator(pair p1,
                pair p2)
{
    return p1.second > p2.second;
}
  
// Function to return maximum
// rating
int FindMaxRating(int N, int Point[],
                  int Upvote[], int K)
{
    // Declaring vector of pairs
    vector > vec;
  
    // Each pair represents a problem
    // with its points and upvotes
    for (int i = 0; i < N; i++)
    {
        vec.push_back(make_pair(Point[i],
                                Upvote[i]));
    }
  
    // Step (1) - Sort problems by their 
    // upvotes value in decreasing order
    sort(vec.begin(), vec.end(), Comparator);
  
    // Declaring min_heap or priority queue 
    // to track of the problem with 
    // minimum points.
    priority_queue,
                  greater > pq;
  
    int total_points = 0, max_rating = 0;
  
    // Step (2) - Loop for i = 0 to K - 1 and 
    // do accordingly
    for (int i = 0; i < K; i++)
    {
        total_points = total_points
                      + vec[i].first;
        max_rating = max(max_rating, 
                         total_points 
                         * vec[i].second);
        pq.push(vec[i].first);
    }
  
    // Step (3) - Loop for i = K to N - 1 
    // and do accordingly
    for (int i = K; i < N; i++)
    {
        if (pq.top() < vec[i].first)
        {
            total_points = total_points
                           - pq.top() 
                           + vec[i].first;
            max_rating = max(max_rating, 
                             total_points
                             * vec[i].second);
              
            pq.pop();
              
            pq.push(vec[i].first);
        }
    }
  
    return max_rating;
}
  
// Driver code
int main()
{
    int Point[] = { 2, 10, 3, 1, 5, 8 };
    int Upvote[] = { 5, 4, 3, 9, 7, 2 };
      
    int N = sizeof(Point) / sizeof(Point[0]);
    int K = 2;
  
    cout << "Maximum Rating of Coding Contest is: "
         << FindMaxRating(N, Point, Upvote, K);
  
    return 0;
}


Python3
# Python3 program to find the Maximum 
# Possible Rating of a Coding Contest 
import heapq
  
# Function to sort all problems 
# descending to upvotes
def Comparator(p1):
      
    return p1[1]
  
# Function to return maximum 
# rating 
def FindMaxRating(N, Point, Upvote, K):
      
    # Declaring vector of pairs
    vec = []
      
    # Each pair represents a problem 
    # with its points and upvotes 
    for i in range(N):
        vec.append([Point[i], Upvote[i]])
      
    # Step (1) - Sort problems by their 
    # upvotes value in decreasing order 
    vec.sort(reverse = True, key = Comparator)
      
    # Declaring min_heap or priority queue 
    # to track of the problem with 
    # minimum points. 
    pq = []
    heapq.heapify(pq)
      
    total_points, max_rating = 0, 0
      
    # Step (2) - Loop for i = 0 to K - 1 and 
    # do accordingly 
    for i in range(K):
        total_points = (total_points + 
                        vec[i][0])
          
        max_rating = max(max_rating,
                         total_points * 
                         vec[i][1])
          
        heapq.heappush(pq, vec[i][0])
      
    # Step (3) - Loop for i = K to N - 1 
    # and do accordingly 
    for i in range(K, N):
        if pq[0] < vec[i][0]:
            total_points = (total_points - 
                                   pq[0] + 
                               vec[i][0])
              
            max_rating = max(max_rating, 
                             total_points * 
                             vec[i][1])
              
            heapq.heappop(pq)
            heapq.heappush(pq, vec[i][0])
              
    return max_rating
  
# Driver code
Point = [ 2, 10, 3, 1, 5, 8 ]
Upvote = [ 5, 4, 3, 9, 7, 2 ]
  
N = len(Point)
K = 2
  
print("Maximum Rating of Coding Contest is:",
       FindMaxRating(N, Point, Upvote, K))
  
# This code is contributed by stutipathak31jan


输出:
Maximum Rating of Coding Contest is: 60

时间复杂度: O(N * logN)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live