给定一个整数N,表示工人] [I]的数目,阵列速度[,在速度表示的第i速度个工人,以及阵列效率[],其中效率[i]表示第i个的效率worker 和一个整数K ,任务是选择K 个工人,使得(所有工人的速度总和)*(K 个工人之间的最低效率)是最大可能的。
例子:
Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 2
Output: 60
Explanation:
Selecting 2nd worker (Speed = 10 and Efficiency = 4) and 5th worker ( Speed = 5 and Efficiency = 7).
Therefore, the maximum sum possible = (10 + 5) * min(4, 7) = 60
Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 3
Output: 68
处理方法:按照以下步骤解决问题:
- 初始化成对向量arr[ ] ,其中arr[i]等于{efficiency[i], speed[i]}大小为N 。
- 排序在降低效率的顺序的ARR []。
- 初始化一个存储工人速度的 min priority_queue。
- 初始化变量说, SumOfSpeed = 0和Ans = 0 。
- 迭代arr[ ]并执行以下操作:
- 将arr[i] .first添加到SumOfSpeed
- 在 priority_queue 中推送speed[i] 。
- 如果 priority_queue 的大小超过K,则弹出 priority_queue 的前面并从SumOfSpeed 中减去。
- 将Ans更新为Ans和SumOfSpeed *efficient[i] 的最大值。
- 最后,返回Ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to generate array of pairs
void generateArrayofPairs(int n, vector& speed,
vector& efficiency,
vector >& arr)
{
for (int i = 0; i < n; i++) {
arr[i] = { efficiency[i], speed[i] };
}
sort(arr.rbegin(), arr.rend());
}
// Function to find the maximum
// product of worker speeds and
// their minimum efficiency
int maximizePerformance(vector& speed, int K,
vector& efficiency)
{
int n = speed.size();
vector > arr(n);
// Function to generate
// sorted array of pairs
generateArrayofPairs(n, speed,
efficiency, arr);
// Initialize priority queue
priority_queue,
greater >
pq;
// Initialize ans and sumofspeed
int ans = 0;
int SumOfSpeed = 0;
// Traversing the arr of pairs
for (auto& it : arr) {
int e = it.first;
int s = it.second;
// Updating sum of speed
SumOfSpeed += s;
// Pushing in priority queue
pq.push(s);
// If team consists of more than
// K workers
if (pq.size() > K) {
int temp = pq.top();
SumOfSpeed -= temp;
pq.pop();
}
// Taking the maximum performance
// that can be formed
ans = max(ans, SumOfSpeed * e);
}
// Finally return the ans
return ans;
}
// Driver Code
int main()
{
// Given Input
vector speed = { 2, 10, 3, 1, 5, 8 };
vector efficiency = { 5, 4, 3, 9, 7, 2 };
int K = 2;
// Function Call
cout << maximizePerformance(
speed, K, efficiency);
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to generate array of pairs
static void generateArrayofPairs(int n, Vector speed,
Vector efficiency,
Vector arr)
{
for (int i = 0; i < n; i++) {
arr.insertElementAt(new pair(efficiency.elementAt(i), speed.elementAt(i)), i);
}
Collections.sort(arr, new Comparator() {
@Override public int compare(pair p1, pair p2)
{
if (p1.first != p2.first)
return (p2.first - p1.first);
return p2.second - p1.second;
}
});
}
// Function to find the maximum
// product of worker speeds and
// their minimum efficiency
static int maximizePerformance(Vector speed, int K,
Vector efficiency)
{
int n = speed.size();
Vector arr = new Vector<>();
// Function to generate
// sorted array of pairs
generateArrayofPairs(n, speed,
efficiency, arr);
// Initialize priority queue
PriorityQueue pq = new PriorityQueue();
// Initialize ans and sumofspeed
int ans = 0;
int SumOfSpeed = 0;
// Traversing the arr of pairs
for (int i = 0; i < arr.size(); i++) {
int e = arr.elementAt(i).first;
int s = arr.elementAt(i).second;
// Updating sum of speed
SumOfSpeed += s;
// Pushing in priority queue
pq.add(s);
// If team consists of more than
// K workers
if (pq.size() > K) {
int temp = pq.peek();
SumOfSpeed -= temp;
pq.poll();
}
// Taking the maximum performance
// that can be formed
ans = Math.max(ans, SumOfSpeed * e);
}
// Finally return the ans
return ans;
}
// Driver Code
public static void main (String[] args) {
// Given Input
Vector speed = new Vector();
speed.add(2);
speed.add(10);
speed.add(3);
speed.add(1);
speed.add(5);
speed.add(8);
Vector efficiency = new Vector();
efficiency.add(5);
efficiency.add(4);
efficiency.add(3);
efficiency.add(9);
efficiency.add(7);
efficiency.add(2);
int K = 2;
// Function Call
System.out.println(maximizePerformance(
speed, K, efficiency));
}
}
// This code is contributed by Dharanendra L V.
输出:
60
时间复杂度: O(NLogN)
辅助空间: O(N)