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

📅  最后修改于: 2021-04-28 17:25:49             🧑  作者: Mango

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

例子:

方法 :

  • 尝试从最高到最低的每次投票的价值,并同时保持尽可能多的积分,将积分一直加到总积分中,如果比赛中的问题数目超过K,则以最低分数解雇该问题。这包括三个步骤。
    1. 根据问题的投票值,以降序对问题进行排序。
    2. 对于索引i = 0、1,…,K-1,我们将这些点推入min_heap并计算等级。我们只需要记录最大评分。我们使用min_heap以最少的分数来跟踪问题。
    3. 对于索引i = K,K + 1,…,N-1,如果当前问题的点大于min_heap的顶部,则弹出现有元素并将当前元素推入min_heap并更新最大值评分。
      这样,由于第min个堆中的K个最大点存在问题,因此就第i个最大投票数的问题计算最大额定值。

下面是上述方法的实现。

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)