📜  有效的送货上门安排总数

📅  最后修改于: 2021-04-29 13:30:02             🧑  作者: Mango

给定订单数量,找到第i个订单始终在提货第i个订单后交付的有效订单排列数。例子:

方法1:

  1. 考虑N = 4,我们总共有8个事件。
  2. 提货{P1,P2,P3,P4}有4个事件,交货{D1,D2,D3,D4}有4个事件。
  3. 如果仅考虑接送事件,则接送之间的安排没有任何限制。因此,总共可能安排4次!
  4. 现在我们考虑交货。我们从上次取车开始。
    • 对于D4,我们只能将D4放在P4之后。
      即P1,P2,P3,P4,__。因此只有1个有效位置。
    • 对于D3,我们可以将D3放在以下任一位置。
      它们是P1,P2,P3,__,P4,__,D4,__。所以3个有效位置。
    • 对于D2,我们可以将D2放在以下任一位置。
      它们是P1,P2,__,P3,__,P4,__,D4,__,D3 __。因此5个有效位置。
    • 对于D1,我们可以将D1放在以下任一位置。
      它们是P1,__,P2,__,P3,__,P4,__,D4,__,D3 __,D2,__。因此有7个有效位置。

    因此,总有效安排为:4! *(1 * 3 * 5 * 7)

对于任何N,总有效安排:

N! * (1 * 3 * 5 * ... * (2 * N - 1))

下面是上述方法的实现。

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to find arrangements
int Arrangements(int N)
{
    int result = 1;
  
    for(int i = 1; i <= N; i++) 
    {
       // Here, i for factorial and
       // (2*i-1) for series 
       result = result * i * (2 * i - 1);
    }
    return result;
}
  
// Driver code
int main()
{
    int N = 4;
  
    cout << Arrangements(N);
  
    return 0;
}


Java
// Java implementation of the above approach 
class GFG{
  
// Function to find arrangements 
public static int Arrangements(int N) 
{ 
    int result = 1; 
      
    for(int i = 1; i <= N; i++)
    { 
  
        // Here, i for factorial and
        // (2*i-1) for series 
       result = result * i * (2 * i - 1); 
    } 
    return result; 
} 
  
// Driver code    
public static void main(String[] args)
{
    int N = 4; 
      
    System.out.print(Arrangements(N));
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation of the above approach
  
# Function to find arrangements
def Arrangements(N):
  
    result = 1
  
    for i in range(1, N + 1):
  
        # Here, i for factorial and
        # (2*i-1) for series 
        result = result * i * (2 * i - 1)
  
    return result
  
# Driver code
N = 4;
print(Arrangements(N));
  
# This code is contributed by Akanksha_Rai


C#
// C# implementation of the above approach 
using System;
  
class GFG{ 
      
// Function to find arrangements 
public static int Arrangements(int N) 
{ 
    int result = 1; 
          
    for(int i = 1; i <= N; i++) 
    { 
  
       // Here, i for factorial and 
       // (2*i-1) for series 
       result = result * i * (2 * i - 1); 
    } 
    return result; 
} 
      
// Driver code 
public static void Main(String[] args) 
{ 
    int N = 4; 
          
    Console.Write(Arrangements(N)); 
} 
} 
  
// This code is contributed by AnkitRai01


C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to find arrangements
int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
int main()
{
    int N = 4;
  
    cout << Arrangements(N);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void main(String args[])
{
    int N = 4;
  
    System.out.print(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the above approach
  
# Function to find arrangements
def Arrangements(N):
    result = 1;
  
    for i in range(1, (2 * N) + 1, 2):
        result = (result * i * (i + 1)) / 2;
  
    return int(result);
  
# Driver code
if __name__ == '__main__':
    N = 4;
  
    print(Arrangements(N));
  
# This code is contributed by gauravrajput1


C#
// C# implementation of the above approach
using System;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void Main()
{
    int N = 4;
  
    Console.Write(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech


输出:
2520

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

方法二:

  1. 对于N个订单,我们有

      Total events = 2 * N

  2. 因此,可能的安排总数为 ( 2 * N )!
  3. 现在,每个订单仅在提货后一次交货才有效。

    对于每个[Pi,Di],我们不能更改此排列,即不能更改[Di,Pi]。每个这样的顺序只有一个有效的排列。因此,我们需要为每个订单除以2。所以总的有效安排是 ( 2 * N )! / 2 ^ N

下面是上述方法的实现。

C++

// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to find arrangements
int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
int main()
{
    int N = 4;
  
    cout << Arrangements(N);
  
    return 0;
}

Java

// Java implementation of the above approach
import java.util.*;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void main(String args[])
{
    int N = 4;
  
    System.out.print(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech

Python3

# Python3 implementation of the above approach
  
# Function to find arrangements
def Arrangements(N):
    result = 1;
  
    for i in range(1, (2 * N) + 1, 2):
        result = (result * i * (i + 1)) / 2;
  
    return int(result);
  
# Driver code
if __name__ == '__main__':
    N = 4;
  
    print(Arrangements(N));
  
# This code is contributed by gauravrajput1

C#

// C# implementation of the above approach
using System;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void Main()
{
    int N = 4;
  
    Console.Write(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech
输出:
2520

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