给定n个已排序文件,任务是找到完成的最少计算以达到最佳合并模式。
当两个或更多排序的文件全部合并在一起形成一个文件时,为达到该文件而进行的最少计算被称为“最佳合并模式” 。
如果需要合并两个以上的文件,则可以成对完成。例如,如果需要合并4个文件A,B,C,D。首先将A与B合并以获取X1,将X1与C合并以获取X2,将X2与D合并以获取X3作为输出文件。
如果我们有两个大小分别为m和n的文件,则总的计算时间将为m + n。在这里,我们通过合并所有存在的文件中两个最小大小的文件来使用贪婪策略。
例子:
给定3个大小为2、3、4个单位的文件。找到组合这些文件的最佳方法
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