📜  根据给定条件恢复洗牌队列

📅  最后修改于: 2021-09-07 04:49:53             🧑  作者: Mango

给定N个人站在队列中和两个数组A[]B[] 。数组 A[] 代表这个人的名字,数组 B[] 代表有多少人比站在那个人前面的某个人高。现在队列被打乱了。任务是按照上述属性打印队列的原始序列。

例子:

方法:

  • 首先制作一对人名及其关联的整数,然后对这些对进行排序。
  • 创建一个数组answer[]来存储人的可能身高。
  • 遍历所有对,如果站在前面的人数高于他们当前的站立位置,则返回-1
  • 否则,将当前站立位置与比他高的人的身高之间的差值存储在答案数组中。
  • 对于每个人迭代配对,如果我们当前人的答案数组的值大于我们与之比较的人,则增加当前配对的答案数组。
  • 最后,根据存储在answer[]数组中的值打印给定序列中可能的对。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to generate the Queue
void OriginalQueue(char A[], int B[],
                   int N)
{
    // Making a pair
    pair a[N + 1];
 
    // Answer array
    int ans[N + 1];
    bool possible = true;
 
    // Store the values in the pair
    for (int i = 0; i < N; i++) {
        a[i].second = A[i];
        a[i].first = B[i];
    }
 
    // Sort the pair
    sort(a, a + N);
 
    // Traverse the pairs
    for (int i = 0; i < N; i++) {
 
        int len = i - a[i].first;
 
        // If it is not possible to
        // generate the Queue
        if (len < 0) {
 
            cout << "-1";
            possible = false;
        }
 
        if (!possible)
            break;
        else {
            ans[i] = len;
 
            for (int j = 0; j < i; j++) {
 
                // Increment the answer
                if (ans[j] >= ans[i])
                    ans[j]++;
            }
        }
 
        // Finally printing the answer
        if (i == N - 1 && possible) {
            for (int i = 0; i < N; i++) {
                cout << a[i].second << " "
                     << ans[i] + 1 << endl;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Given name of person as char
    char A[N] = { 'a', 'b', 'c', 'd' };
 
    // Associated integers
    int B[N] = { 0, 2, 0, 0 };
 
    // Function Call
    OriginalQueue(A, B, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to generate the Queue
static void OriginalQueue(char A[], int B[],
                                    int N)
{
    // Making a pair
    int[][] a = new int[N][2];
 
    // Answer array
    int[] ans = new int[N];
    boolean possible = true;
 
    // Store the values in the pair
    for(int i = 0; i < N; i++)
    {
        a[i][0] = B[i];
        a[i][1] = (int)A[i];
    }
 
    // Sort the pair
    Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
     
    // Traverse the pairs
    for(int i = 0; i < N; i++)
    {
        int len = i - a[i][0];
 
        // If it is not possible to
        // generate the Queue
        if (len < 0)
        {
            System.out.print("-1");
            possible = false;
        }
 
        if (!possible)
            break;
        else
        {
            ans[i] = len;
 
            for(int j = 0; j < i; j++)
            {
 
                // Increment the answer
                if (ans[j] >= ans[i])
                    ans[j]++;
            }
        }
 
        // Finally printing the answer
        if (i == N - 1 && possible)
        {
            for(int k = 0; k < N; k++)
            {
                System.out.println((char)a[k][1] +
                                 " "+ (ans[k] + 1));
            }
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 4;
     
    // Given name of person as char
    char A[] = { 'a', 'b', 'c', 'd' };
     
    // Associated integers
    int B[] = { 0, 2, 0, 0 };
     
    // Function Call
    OriginalQueue(A, B, N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to generate the Queue
def OriginalQueue(A, B, N):
     
    # Making a pair
    a = [[0, ""] for i in range(N)]
 
    # Answer array
    ans = [0 for i in range(N)]
    possible = True
 
    # Store the values in the pair
    for i in range(N):
        a[i][1] = str(A[i])
        a[i][0] = B[i]
 
    # Sort the pair
    a.sort(reverse = False)
 
    # Traverse the pairs
    for i in range(N):
        len1 = i - a[i][0]
 
        # If it is not possible to
        # generate the Queue
        if (len1 < 0):
            print("-1",end = "")
            possible = False
 
        if (possible == False):
            break
        else:
            ans[i] = len1
 
            for j in range(i):
                 
                # Increment the answer
                if (ans[j] >= ans[i]):
                    ans[j] += 1
 
        # Finally printing the answer
        if (i == N - 1 and possible):
            for i in range(N):
                print(a[i][1], ans[i] + 1)
 
# Driver Code
if __name__ == '__main__':
     
    N = 4
 
    # Given name of person as char
    A = [ 'a', 'b', 'c', 'd' ]
 
    # Associated integers
    B = [ 0, 2, 0, 0 ]
 
    # Function Call
    OriginalQueue(A, B, N)
 
# This code is contributed by ipg2016107


Javascript


输出:
a 1
c 3
d 4
b 2

时间复杂度: O(N 2 )
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live