给定 n 个已排序文件,任务是找到为达到最佳合并模式所做的最小计算。
当两个或多个排序文件要合并在一起形成一个文件时,为到达该文件所做的最少计算称为最佳合并模式。
如果需要合并 2 个以上的文件,则可以成对完成。例如,如果需要合并4个文件A、B、C、D。首先合并A和B得到X1,合并X1和C得到X2,合并X2和D得到X3作为输出文件。
如果我们有两个大小为 m 和 n 的文件,则总计算时间将为 m+n。在这里,我们通过在所有存在的文件中合并两个最小大小的文件来使用贪婪策略。
例子:
给定大小为 2、3、4 个单位的 3 个文件。找到组合这些文件的最佳方式
Input: n = 3, size = {2, 3, 4}
Output: 14
Explanation: There are different ways to combine these files:
Method 1: Optimal method
Method 2:
Method 3:
Input: n = 6, size = {2, 3, 4, 5, 6, 7}
Output: 68
Explanation: Optimal way to combine these files
方法:
Node represents a file with a given size also given nodes are greater than 2
- Add all the nodes in a priority queue (Min Heap).{node.weight = file size}
- Initialize count = 0 // variable to store file computations.
- Repeat while (size of priority Queue is greater than 1)
- create a new node
- new node = pq.poll().weight+pq.poll().weight;//pq denotes priority queue, remove 1st smallest and 2nd smallest element and add their weights to get a new node
- count += node.wight
- add this new node to priority queue;
- count is the final answer
下面是上述方法的实现:
C++
// C++ program to implement
// Optimal File Merge Pattern
#include
using namespace std;
// Function to find minimum computation
int minComputation(int size, int files[])
{
// Create a min heap
priority_queue,
greater> pq;
for(int i = 0; i < size; i++)
{
// Add sizes to priorityQueue
pq.push(files[i]);
}
// Variable to count total Computation
int count = 0;
while(pq.size() > 1)
{
// pop two smallest size element
// from the min heap
int first_smallest = pq.top();
pq.pop();
int second_smallest = pq.top();
pq.pop();
int temp = first_smallest + second_smallest;
// Add the current computations
// with the previous one's
count += temp;
// Add new combined file size
// to priority queue or min heap
pq.push(temp);
}
return count;
}
// Driver code
int main()
{
// No of files
int n = 6;
// 6 files with their sizes
int files[] = { 2, 3, 4, 5, 6, 7 };
// Total no of computations
// do be done final answer
cout << "Minimum Computations = "
<< minComputation(n, files);
return 0;
}
// This code is contributed by jaigoyal1328
Java
// Java program to implement
// Optimal File Merge Pattern
import java.util.Scanner;
import java.util.PriorityQueue;
public class OptimalMergePatterns {
// Function to find minimum computation
static int minComputation(int size, int files[])
{
// create a min heap
PriorityQueue pq
= new PriorityQueue<>();
for (int i = 0; i < size; i++) {
// add sizes to priorityQueue
pq.add(files[i]);
}
// variable to count total computations
int count = 0;
while (pq.size() > 1) {
// pop two smallest size element
// from the min heap
int temp = pq.poll() + pq.poll();
// add the current computations
// with the previous one's
count += temp;
// add new combined file size
// to priority queue or min heap
pq.add(temp);
}
return count;
}
public static void main(String[] args)
{
// no of files
int size = 6;
// 6 files with their sizes
int files[] = new int[] { 2, 3, 4, 5, 6, 7 };
// total no of computations
// do be done final answer
System.out.println("Minimum Computations = "
+ minComputation(size, files));
}
}
Minimum Computations = 68
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。