📜  磁带上的垂直和水平检索(MRT)

📅  最后修改于: 2021-05-06 21:16:59             🧑  作者: Mango

先决条件:磁带上的最佳存储
您将获得不同大小的tapes []和不同卷的记录[] 。任务是水平或垂直地在磁带中填充卷,以使“检索时间”最小化。
例子:

对于“最小检索时间”,我们将按其大小的升序插入记录量。
最小检索时间的公式为:

MRT = (1/N)*Σ(A(i)*(N-i))
where 0 ≤ i < N

有两种方法可以在磁带上添加卷以减少检索时间:

  1. 水平检索
  2. 垂直检索

水平检索

我们有,
磁带[] = {25,80,160}
records [] = {2,8,15,23,45,50,60,120} (按升序排列)
在胶带上水平填充:

  1. 插入2
    1st tape : [ 2 ]
    2nd tape : [ ]
    3rd tape : [ ]
    
  2. 插入8
    1st tape : [ 2 8 ]
    2nd tape : [ ]
    3rd tape : [ ]
    
  3. 插入15
    1st tape : [ 2 8 15 ]
    2nd tape : [ ]
    3rd tape : [ ]
    
  4. 插入23
    1st tape : [ 2 8 15 ]
    2nd tape : [ 23 ]
    3rd tape : [ ]
    
  5. 插入45
    1st tape : [ 2 8 15 ]
    2nd tape : [ 23 45 ]
    3rd tape : [ ]
    
  6. 插入50
    1st tape : [ 2 8 15 ]
    2nd tape : [ 23 45 ]
    3rd tape : [ 50 ]
    
  7. 插入60
    1st tape : [ 2 8 15 ]
    2nd tape : [ 23 45 ]
    3rd tape : [ 50 60 ]
    

以下是水平灌装程序

C++
// C++ program to print Horizontal
// filling
#include 
using namespace std;
  
void horizontalFill(int records[],
                    int tape[], int nt)
{
    // It is used for checking whether
    // tape is full or not
    int sum = 0;
  
    // It is used for calculating
    // total retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating
    // mean retrieval time
    double Mrt;
    int current = 0;
  
    // vector is used because n number of
    // records can insert in one tape with
    // size constraint
    vector v;
  
    for (int i = 0; i < nt; i++) {
  
        // Null vector obtained to use fresh
        // vector 'v'
        v.clear();
        Retrieval_Time = 0;
  
        // initialize variables to
        // 0 for each iteration
        sum = 0;
        cout << "tape " << i + 1 << " : [ ";
  
        // sum is used for checking whether
        // i'th tape is full or not
        sum += records[current];
  
        // check sum less than size of tape
        while (sum <= tape[i]) {
            cout << records[current] << " ";
            v.push_back(records[current]);
  
            // increment in j for next record
            current++;
            sum += records[current];
        }
        cout << "]";
  
        // calculating total retrieval
        // time
        for (int i = 0; i < v.size(); i++) {
  
            // MRT formula
            Retrieval_Time += v[i] * (v.size() - i);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / v.size();
  
        // v.size() is function of vector is used
        // to get size of vector
        cout << "\tMRT : " << Mrt << endl;
    }
}
  
// Driver Code
int main()
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = sizeof(records) / sizeof(records[0]);
  
    // store the size of tapes[]
    int m = sizeof(tape) / sizeof(tape[0]);
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    sort(records, records + n);
    horizontalFill(records, tape, m);
}


Java
// Java program to print Horizontal
// filling
import java.util.*;
  
class GFG
{
  
static void horizontalFill(int records[],
                    int tape[], int nt)
{
    // It is used for checking whether
    // tape is full or not
    int sum = 0;
  
    // It is used for calculating
    // total retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating
    // mean retrieval time
    double Mrt;
    int current = 0;
  
    // vector is used because n number of
    // records can insert in one tape with
    // size constraint
    Vector v = new Vector();
  
    for (int i = 0; i < nt; i++)
    {
  
        // Null vector obtained to use fresh
        // vector 'v'
        v.clear();
        Retrieval_Time = 0;
  
        // initialize variables to
        // 0 for each iteration
        sum = 0;
        System.out.print("tape " + (i + 1) + " : [ ");
  
        // sum is used for checking whether
        // i'th tape is full or not
        sum += records[current];
  
        // check sum less than size of tape
        while (sum <= tape[i])
        {
            System.out.print(records[current] + " ");
            v.add(records[current]);
  
            // increment in j for next record
            current++;
            sum += records[current];
        }
        System.out.print("]");
  
        // calculating total retrieval
        // time
        for (int j = 0; j < v.size(); j++) 
        {
  
            // MRT formula
            Retrieval_Time += v.get(j) * (v.size() - j);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / v.size();
  
        // v.size() is function of vector is used
        // to get size of vector
        System.out.print("\tMRT : " + Mrt +"\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = records.length;
  
    // store the size of tapes[]
    int m = tape.length;
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Arrays.sort(records);
    horizontalFill(records, tape, m);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to print Horizontal 
# filling 
  
def horizontalFill(records,  tape, nt) : 
  
    # It is used for checking whether 
    # tape is full or not 
    sum = 0; 
  
    # It is used for calculating 
    # total retrieval time 
    Retrieval_Time = 0; 
  
    # It is used for calculating 
    # mean retrieval time 
    current = 0; 
  
    # vector is used because n number of 
    # records can insert in one tape with 
    # size constraint 
    v = []; 
  
    for i in range(nt) :
  
        # Null vector obtained to use fresh 
        # vector 'v' 
        v.clear(); 
        Retrieval_Time = 0; 
  
        # initialize variables to 
        # 0 for each iteration 
        sum = 0; 
        print("tape" , i + 1  ,": [ ",end =""); 
  
        # sum is used for checking whether 
        # i'th tape is full or not 
        sum += records[current]; 
  
        # check sum less than size of tape 
        while (sum <= tape[i]) :
            print(records[current],end= " "); 
            v.append(records[current]); 
  
            # increment in j for next record 
            current += 1; 
            sum += records[current]; 
  
        print("]",end=""); 
  
        # calculating total retrieval 
        # time 
        for i in range(len(v)) :
  
            # MRT formula 
            Retrieval_Time += v[i] * (len(v) - i); 
      
  
        # calculating mean retrieval time 
        # using formula 
        Mrt = Retrieval_Time / len(v); 
  
        # v.size() is function of vector is used 
        # to get size of vector 
        print("tMRT :", Mrt); 
  
# Driver Code 
if __name__ == "__main__" : 
  
    records = [ 15, 2, 8, 23, 45, 50, 60, 120 ];
    tape = [ 25, 80, 160 ];
      
    # store the size of records[]
    n = len(records);
      
    # store the size of tapes[]
    m = len(tape);
    # sorting of an array is required to 
    # attain greedy approach of
    # algorithm
    records.sort();
    horizontalFill(records, tape, m); 
  
# This code is contributed by AnkitRai01


C#
// C# program to print Horizontal
// filling
using System;
using System.Collections.Generic;
  
class GFG
{
   
static void horizontalFill(int []records,
                    int []tape, int nt)
{
    // It is used for checking whether
    // tape is full or not
    int sum = 0;
   
    // It is used for calculating
    // total retrieval time
    int Retrieval_Time = 0;
   
    // It is used for calculating
    // mean retrieval time
    double Mrt;
    int current = 0;
   
    // vector is used because n number of
    // records can insert in one tape with
    // size constraint
    List v = new List();
   
    for (int i = 0; i < nt; i++)
    {
   
        // Null vector obtained to use fresh
        // vector 'v'
        v.Clear();
        Retrieval_Time = 0;
   
        // initialize variables to
        // 0 for each iteration
        sum = 0;
        Console.Write("tape " + (i + 1) + " : [ ");
   
        // sum is used for checking whether
        // i'th tape is full or not
        sum += records[current];
   
        // check sum less than size of tape
        while (sum <= tape[i])
        {
            Console.Write(records[current] + " ");
            v.Add(records[current]);
   
            // increment in j for next record
            current++;
            sum += records[current];
        }
        Console.Write("]");
   
        // calculating total retrieval
        // time
        for (int j = 0; j < v.Count; j++) 
        {
   
            // MRT formula
            Retrieval_Time += v[j] * (v.Count - j);
        }
   
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / v.Count;
   
        // v.Count is function of vector is used
        // to get size of vector
        Console.Write("\tMRT : " + Mrt +"\n");
    }
}
   
// Driver Code
public static void Main(String[] args)
{
    int []records = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int []tape = { 25, 80, 160 };
   
    // store the size of records[]
    int n = records.Length;
   
    // store the size of tapes[]
    int m = tape.Length;
   
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Array.Sort(records);
    horizontalFill(records, tape, m);
}
}
  
// This code is contributed by 29AjayKumar


C++
// C++ program to print Vertical filling
#include 
using namespace std;
  
void vertical_Fill(int records[], int tape[],
                   int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int v[m][n] = { 0 };
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) {
                sum += v[j][k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j]) {
                v[j][i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2][i] == 0) {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        cout << "tape " << i + 1 << " : [ ";
        for (j = 0; j < n; j++) {
  
            if (v[i][j] != 0) {
                cout << v[i][j] << " ";
            }
            else {
                break;
            }
        }
        cout << "]";
  
        // calculating total retrieval time
        for (int k = 0; v[i][k] != 0; k++) {
            // MRT formula
            Retrieval_Time += v[i][k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.size() is function of vector is used
        // to get size of vector
        cout << "\tMRT : " << Mrt << endl;
    }
}
  
// Driver Code
int main()
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = sizeof(records) / sizeof(records[0]);
  
    // store the size of tapes[]
    int m = sizeof(tape) / sizeof(tape[0]);
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    sort(records, records + n);
    vertical_Fill(records, tape, m, n);
    return 0;
}


Java
// Java program to print Vertical filling
import java.util.*;
  
class GFG
{
  
static void vertical_Fill(int records[], int tape[],
                        int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int [][]v = new int[m][n];
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) 
            {
                sum += v[j][k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j])
            {
                v[j][i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2][i] == 0) 
        {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) 
    {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        System.out.print("tape " + (i + 1)+ " : [ ");
        for (j = 0; j < n; j++)
        {
  
            if (v[i][j] != 0)
            {
                System.out.print(v[i][j]+ " ");
            }
            else
            {
                break;
            }
        }
        System.out.print("]");
  
        // calculating total retrieval time
        for (int k = 0; v[i][k] != 0; k++)
        {
              
            // MRT formula
            Retrieval_Time += v[i][k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.size() is function of vector is used
        // to get size of vector
        System.out.print("\tMRT : " + Mrt +"\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = records.length;
  
    // store the size of tapes[]
    int m = tape.length;
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Arrays.sort(records);
    vertical_Fill(records, tape, m, n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to print Vertical filling 
import numpy as np
  
def vertical_Fill(records, tape, m, n) : 
  
    # 2D matrix for vertical insertion on 
    # tapes 
    v = np.zeros((m, n)) ;
  
    # It is used for checking whether tape 
    # is full or not 
    sum = 0; 
  
    # It is used for calculating total 
    # retrieval time 
    Retrieval_Time = 0; 
  
    # It is used for calculating mean 
    # retrieval time 
    Mrt = None; 
  
    # It is used for calculating mean 
    # retrieval time 
    z = 0; j = 0; 
  
    # vertical insertion on tape 
    for i in range(n) :
        for j in range(m) :
  
            # initialize variables to 
            # 0 for each iteration 
            sum = 0; 
  
            # Used for getting 'sum' sizes 
            # of records in tape to determine 
            # whether tape is full or not 
            for k in range(i) :
                sum += v[j][k]; 
  
            # if tape is not full 
            if (sum + records[z] <= tape[j]) :
                v[j][i] = records[z]; 
                z += 1; 
  
        # check for ability of tapes to hold 
        # value 
        if (v[2][i] == 0) :
            break; 
  
    for i in range(m) :
  
        # initialize variables to 0 for each 
        # iteration 
        Retrieval_Time = 0; 
  
        # display elements of tape 
        print("tape" , i + 1 ,": [", end = ""); 
        for j in range(n) :
  
            if (v[i][j] != 0) :
                print(v[i][j], end= " "); 
      
            else :
                break; 
              
        print("]", end="");
          
        k = 0;
          
        # calculating total retrieval time 
        while v[i][k] != 0 :
          
            # MRT formula 
            Retrieval_Time += v[i][k] * (j - k);
            k += 1;
              
        # calculating mean retrieval time
        # using formula
        Mrt = Retrieval_Time / j; 
  
        # v.size() is function of vector is used 
        # to get size of vector 
        print("MRT :", Mrt);
  
# Driver Code 
if __name__ == "__main__" : 
  
    records = [ 15, 2, 8, 23, 45, 50, 60, 120 ]; 
    tape = [ 25, 80, 160 ]; 
  
    # store the size of records[] 
    n = len(records); 
  
    # store the size of tape[] 
    m = len(tape); 
  
    # sorting of an array is required to 
    # attain greedy approach of 
    # algorithm 
    records.sort(); 
    vertical_Fill(records, tape, m, n);
  
# This code is contributed by AnkitRai01


C#
// C# program to print Vertical filling
using System;
  
class GFG
{
  
static void vertical_Fill(int []records, int []tape,
                        int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int [,]v = new int[m, n];
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) 
            {
                sum += v[j, k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j])
            {
                v[j, i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2, i] == 0) 
        {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) 
    {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        Console.Write("tape " + (i + 1) + " : [ ");
        for (j = 0; j < n; j++)
        {
  
            if (v[i, j] != 0)
            {
                Console.Write(v[i, j]+ " ");
            }
            else
            {
                break;
            }
        }
        Console.Write("]");
  
        // calculating total retrieval time
        for (int k = 0; v[i, k] != 0; k++)
        {
              
            // MRT formula
            Retrieval_Time += v[i, k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.Count is function of vector is used
        // to get size of vector
        Console.Write("\tMRT : " + Mrt +"\n");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []records = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int []tape = { 25, 80, 160 };
  
    // store the size of records[]
    int n = records.Length;
  
    // store the size of tapes[]
    int m = tape.Length;
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Array.Sort(records);
    vertical_Fill(records, tape, m, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
tape 1 : [ 2 8 15 ]    MRT : 12.3333
tape 2 : [ 23 45 ]    MRT : 45.5
tape 3 : [ 50 60 ]    MRT : 80

垂直检索

胶带上的垂直填充:

  1. 插入2
    1st tape : [ 2 ]
    2nd tape : [ ]
    3rd tape : [ ]
    
  2. 插入8
    1st tape : [ 2 ]
    2nd tape : [ 8 ]
    3rd tape : [ ]
    
  3. 插入15
    1st tape : [ 2 ]
    2nd tape : [ 8 ]
    3rd tape : [ 15 ]
    
  4. 插入23
    1st tape : [ 2 23 ]
    2nd tape : [ 8 ]
    3rd tape : [ 15 ]
    
  5. 插入45
    1st tape : [ 2 23 ]
    2nd tape : [ 8 45 ]
    3rd tape : [ 15 ]
    
  6. 插入50
    1st tape : [ 2 23 ]
    2nd tape : [ 8 45 ]
    3rd tape : [ 15 50 ]
    
  7. 插入60
    1st tape : [ 2 23 60 ]
    2nd tape : [ 8 45 ]
    3rd tape : [ 15 50 ]
    

以下是垂直填充程序

C++

// C++ program to print Vertical filling
#include 
using namespace std;
  
void vertical_Fill(int records[], int tape[],
                   int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int v[m][n] = { 0 };
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) {
                sum += v[j][k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j]) {
                v[j][i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2][i] == 0) {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        cout << "tape " << i + 1 << " : [ ";
        for (j = 0; j < n; j++) {
  
            if (v[i][j] != 0) {
                cout << v[i][j] << " ";
            }
            else {
                break;
            }
        }
        cout << "]";
  
        // calculating total retrieval time
        for (int k = 0; v[i][k] != 0; k++) {
            // MRT formula
            Retrieval_Time += v[i][k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.size() is function of vector is used
        // to get size of vector
        cout << "\tMRT : " << Mrt << endl;
    }
}
  
// Driver Code
int main()
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = sizeof(records) / sizeof(records[0]);
  
    // store the size of tapes[]
    int m = sizeof(tape) / sizeof(tape[0]);
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    sort(records, records + n);
    vertical_Fill(records, tape, m, n);
    return 0;
}

Java

// Java program to print Vertical filling
import java.util.*;
  
class GFG
{
  
static void vertical_Fill(int records[], int tape[],
                        int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int [][]v = new int[m][n];
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) 
            {
                sum += v[j][k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j])
            {
                v[j][i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2][i] == 0) 
        {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) 
    {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        System.out.print("tape " + (i + 1)+ " : [ ");
        for (j = 0; j < n; j++)
        {
  
            if (v[i][j] != 0)
            {
                System.out.print(v[i][j]+ " ");
            }
            else
            {
                break;
            }
        }
        System.out.print("]");
  
        // calculating total retrieval time
        for (int k = 0; v[i][k] != 0; k++)
        {
              
            // MRT formula
            Retrieval_Time += v[i][k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.size() is function of vector is used
        // to get size of vector
        System.out.print("\tMRT : " + Mrt +"\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int tape[] = { 25, 80, 160 };
  
    // store the size of records[]
    int n = records.length;
  
    // store the size of tapes[]
    int m = tape.length;
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Arrays.sort(records);
    vertical_Fill(records, tape, m, n);
}
}
  
// This code is contributed by 29AjayKumar

Python3

# Python3 program to print Vertical filling 
import numpy as np
  
def vertical_Fill(records, tape, m, n) : 
  
    # 2D matrix for vertical insertion on 
    # tapes 
    v = np.zeros((m, n)) ;
  
    # It is used for checking whether tape 
    # is full or not 
    sum = 0; 
  
    # It is used for calculating total 
    # retrieval time 
    Retrieval_Time = 0; 
  
    # It is used for calculating mean 
    # retrieval time 
    Mrt = None; 
  
    # It is used for calculating mean 
    # retrieval time 
    z = 0; j = 0; 
  
    # vertical insertion on tape 
    for i in range(n) :
        for j in range(m) :
  
            # initialize variables to 
            # 0 for each iteration 
            sum = 0; 
  
            # Used for getting 'sum' sizes 
            # of records in tape to determine 
            # whether tape is full or not 
            for k in range(i) :
                sum += v[j][k]; 
  
            # if tape is not full 
            if (sum + records[z] <= tape[j]) :
                v[j][i] = records[z]; 
                z += 1; 
  
        # check for ability of tapes to hold 
        # value 
        if (v[2][i] == 0) :
            break; 
  
    for i in range(m) :
  
        # initialize variables to 0 for each 
        # iteration 
        Retrieval_Time = 0; 
  
        # display elements of tape 
        print("tape" , i + 1 ,": [", end = ""); 
        for j in range(n) :
  
            if (v[i][j] != 0) :
                print(v[i][j], end= " "); 
      
            else :
                break; 
              
        print("]", end="");
          
        k = 0;
          
        # calculating total retrieval time 
        while v[i][k] != 0 :
          
            # MRT formula 
            Retrieval_Time += v[i][k] * (j - k);
            k += 1;
              
        # calculating mean retrieval time
        # using formula
        Mrt = Retrieval_Time / j; 
  
        # v.size() is function of vector is used 
        # to get size of vector 
        print("MRT :", Mrt);
  
# Driver Code 
if __name__ == "__main__" : 
  
    records = [ 15, 2, 8, 23, 45, 50, 60, 120 ]; 
    tape = [ 25, 80, 160 ]; 
  
    # store the size of records[] 
    n = len(records); 
  
    # store the size of tape[] 
    m = len(tape); 
  
    # sorting of an array is required to 
    # attain greedy approach of 
    # algorithm 
    records.sort(); 
    vertical_Fill(records, tape, m, n);
  
# This code is contributed by AnkitRai01

C#

// C# program to print Vertical filling
using System;
  
class GFG
{
  
static void vertical_Fill(int []records, int []tape,
                        int m, int n)
{
    // 2D matrix for vertical insertion on
    // tapes
    int [,]v = new int[m, n];
  
    // It is used for checking whether tape
    // is full or not
    int sum = 0;
  
    // It is used for calculating total
    // retrieval time
    int Retrieval_Time = 0;
  
    // It is used for calculating mean
    // retrieval time
    double Mrt;
  
    // It is used for calculating mean
    // retrieval time
    int z = 0, j = 0;
  
    // vertical insertion on tape
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
  
            // initialize variables to
            // 0 for each iteration
            sum = 0;
  
            // Used for getting 'sum' sizes
            // of records in tape to determine
            // whether tape is full or not
            for (int k = 0; k < i; k++) 
            {
                sum += v[j, k];
            }
  
            // if tape is not full
            if (sum + records[z] <= tape[j])
            {
                v[j, i] = records[z];
                z++;
            }
        }
  
        // check for ability of tapes to hold
        // value
        if (v[2, i] == 0) 
        {
            break;
        }
    }
  
    for (int i = 0; i < m; i++) 
    {
  
        // initialize variables to 0 for each
        // iteration
        Retrieval_Time = 0;
  
        // display elements of tape
        Console.Write("tape " + (i + 1) + " : [ ");
        for (j = 0; j < n; j++)
        {
  
            if (v[i, j] != 0)
            {
                Console.Write(v[i, j]+ " ");
            }
            else
            {
                break;
            }
        }
        Console.Write("]");
  
        // calculating total retrieval time
        for (int k = 0; v[i, k] != 0; k++)
        {
              
            // MRT formula
            Retrieval_Time += v[i, k] * (j - k);
        }
  
        // calculating mean retrieval time
        // using formula
        Mrt = (double)Retrieval_Time / j;
  
        // v.Count is function of vector is used
        // to get size of vector
        Console.Write("\tMRT : " + Mrt +"\n");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []records = { 15, 2, 8, 23, 45, 50, 60, 120 };
    int []tape = { 25, 80, 160 };
  
    // store the size of records[]
    int n = records.Length;
  
    // store the size of tapes[]
    int m = tape.Length;
  
    // sorting of an array is required to
    // attain greedy approach of
    // algorithm
    Array.Sort(records);
    vertical_Fill(records, tape, m, n);
}
}
  
// This code is contributed by 29AjayKumar
输出:
tape 1 : [ 2 23 ]    MRT : 13.5
tape 2 : [ 8 45 ]    MRT : 30.5
tape 3 : [ 15 50 60 ]    MRT : 68.3333

比较两个结果,我们可以使用一种特定的胶带填充技术,该技术可以提供最佳的结果(最小MRT)。