📜  磁带上的最佳存储

📅  最后修改于: 2021-10-25 06:28:43             🧑  作者: Mango

给定的n 存储在计算机磁带上的程序和每个程序的长度i L_i 在哪里1<=i<=n , 找出程序应存储在平均检索时间(MRT 为\frac{1}{n} \sum_{i=1}^{n} \sum_{j=1}^{i} L_j ) 最小化。
例子:

Input : n = 3
        L[] = { 5, 3, 10 }
Output : Order should be { 3, 5, 10 } with MRT = 29/3

先决条件:磁带数据存储

让我们首先分解问题并了解需要做什么。
磁带仅提供数据的顺序访问。在录音磁带/磁带中,与 CD 不同,不能直接播放磁带中的第五首歌曲。必须遍历前四首歌曲的长度才能播放第五首歌曲。因此,为了访问某些数据,应相应地定位磁带头。
现在假设音频长度分别为 5、7、3 和 2 分钟的磁带中有 4 首歌曲。为了播放第四首歌曲,我们需要遍历 5 + 7 + 3 = 15 分钟的音频长度,然后定位磁带头。
数据的检索时间是检索/访问整个数据所花费的时间。因此,第四首歌曲的检索时间为 15 + 2 = 17 分钟。
现在,考虑到磁带中所有程序的检索频率相同,并且每次磁带头都指向磁带的前端,因此可以定义一个新术语,称为平均检索时间 (MRT)。
让我们假设程序的检索时间i T_i .所以, T_i = \sum_{j=1}^{i} L_j
MRT 是所有这些的平均值T_i .所以MRT = \frac{1}{n} \sum_{i=1}^{n} T_i , 或者MRT = \frac{1}{n} \sum_{i=1}^{n} \sum_{j=1}^{i} L_j
磁带中数据的顺序访问有一些限制。必须定义磁带中数据/程序的存储顺序,以便获得最少的 MRT。因此,存储顺序对于减少数据检索/访问时间变得非常重要。
因此,任务减少了——定义正确的顺序,从而最小化 MRT,即最小化项\sum_{i=1}^{n} \sum_{j=1}^{i} L_i
例如,假设有 3 个长度分别为 2、5 和 4 的程序。所以一共有3个! = 6 个可能的存储顺序。

  Order Total Retrieval Time Mean Retrieval Time
1 1 2 3 2 + (2 + 5) + (2 + 5 + 4) = 20 20/3
2 1 3 2 2 + (2 + 4) + (2 + 4 + 5) = 19 19/3
3 2 1 3 5 + (5 + 2) + (5 + 2 + 4) = 23 23/3
4 2 3 1 5 + (5 + 4) + (5 + 4 + 2) = 25 25/3
5 3 1 2 4 + (4 + 2) + (4 + 2 + 5) = 21 21/3
6 3 2 1 4 + (4 + 5) + (4 + 5 + 2) = 24 24/3

很明显,按照第二个顺序存储程序,平均检索时间最短。
在上面的例子中,第一个程序的长度被添加’n’次,第二个’n-1’次……直到最后一个程序只添加一次。因此,仔细分析表明,为了最小化 MRT,应该将长度更长的程序放在最后,以便减少总和。或者,程序的长度应按递增顺序排序。这就是使用的贪心算法——在每一步,我们都会立即选择将时间最少的程序放在第一位,以便逐个构建问题的最终优化解决方案。
下面是实现:

C++
// CPP Program to find the order
// of programs for which MRT is
// minimized
#include 
 
using namespace std;
 
// This functions outputs the required
// order and Minimum Retrieval Time
void findOrderMRT(int L[], int n)
{
    // Here length of i'th program is L[i]
    sort(L, L + n);
 
    // Lengths of programs sorted according to increasing
    // lengths. This is the order in which the programs
    // have to be stored on tape for minimum MRT
    cout << "Optimal order in which programs are to be"
            "stored is: ";
    for (int i = 0; i < n; i++)
        cout << L[i] << " ";
    cout << endl;
 
    // MRT - Minimum Retrieval Time
    double MRT = 0;
    for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = 0; j <= i; j++)
            sum += L[j];
        MRT += sum;
    }
    MRT /= n;
    cout << "Minimum Retrieval Time of this"
           " order is " << MRT;
}
 
// Driver Code to test above function
int main()
{
    int L[] = { 2, 5, 4 };
    int n = sizeof(L) / sizeof(L[0]);
    findOrderMRT(L, n);
    return 0;
}


Java
// Java Program to find the order
// of programs for which MRT is
// minimized
import java.io.*;
import java .util.*;
 
class GFG
{
 
// This functions outputs
// the required order and
// Minimum Retrieval Time
static void findOrderMRT(int []L,
                         int n)
{
    // Here length of
    // i'th program is L[i]
    Arrays.sort(L);
 
    // Lengths of programs sorted
    // according to increasing lengths.
    // This is the order in which
    // the programs have to be stored
    // on tape for minimum MRT
    System.out.print("Optimal order in which " +
              "programs are to be stored is: ");
    for (int i = 0; i < n; i++)
        System.out.print(L[i] + " ");
        System.out.println();
 
    // MRT - Minimum Retrieval Time
    double MRT = 0;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j <= i; j++)
            sum += L[j];
        MRT += sum;
    }
    MRT /= n;
    System.out.print( "Minimum Retrieval Time" +
                    " of this order is " + MRT);
}
 
// Driver Code
public static void main (String[] args)
{
    int []L = { 2, 5, 4 };
    int n = L.length;
    findOrderMRT(L, n);
}
}
 
// This code is contributed
// by anuj_67.


C#
// C# Program to find the
// order of programs for
// which MRT is minimized
using System;
 
class GFG
{
 
// This functions outputs
// the required order and
// Minimum Retrieval Time
static void findOrderMRT(int []L,
                         int n)
{
    // Here length of
    // i'th program is L[i]
    Array.Sort(L);
 
    // Lengths of programs sorted
    // according to increasing lengths.
    // This is the order in which
    // the programs have to be stored
    // on tape for minimum MRT
    Console.Write("Optimal order in " +  
                  "which programs are" +
                  " to be stored is: ");
    for (int i = 0; i < n; i++)
        Console.Write(L[i] + " ");
        Console.WriteLine();
 
    // MRT - Minimum Retrieval Time
    double MRT = 0;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j <= i; j++)
            sum += L[j];
        MRT += sum;
    }
    MRT /= n;
    Console.WriteLine("Minimum Retrieval " +
                  "Time of this order is " +
                                       MRT);
}
 
// Driver Code
public static void Main ()
{
    int []L = { 2, 5, 4 };
    int n = L.Length;
    findOrderMRT(L, n);
}
}
 
// This code is contributed
// by anuj_67.


Javascript


输出:

Optimal order in which programs are to be stored are: 2 4 5 
Minimum Retrieval Time of this order is 6.33333

上述程序的时间复杂度就是排序的时间复杂度,即O(n lgn) (由于 std::sort() 在O(n lgn) ) 如果您使用冒泡排序而不是 std::sort(),则需要O(n^2)
您可能认为上述特定代码的时间复杂度应该是由于 ‘mrt’ 计算中的两个循环,即, O(n^2) ,但请记住,直觉上,所使用的 for 循环也可以用这种方式编码以避免两个循环:

for (int i = 0; i < n; i++)
    MRT += (n - i) * L[i];

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程