给定订单数量,找到第i个订单始终在提货第i个订单后交付的有效订单排列数。
例子:
Input: N = 1
Output: 1
Here total event is 2. They are {P1, D1}.
Total possible arrangement is 2! = 2. [P1, D1] and [D1, P1].
So only valid arrangement possible: [P1, D1].
[D1, P1] is invalid arrangement as delivery of 1st order is done before pickup of 1st order.
Input: N = 2
Output: 6
Here total event is 4. They are {P1, D1, P2, D2}.
Here total possible arrangements are 4! = 24.
Among them 6 are valid arrangements:
[P1, P2, D1, D2], [P1, D1, P2, D2], [P1, P2, D2, D1], [P2, P1, D2, D1], [P2, P1, D1, D2], and [P2, D2, P1, D1].
Rest all are invalid arrangements.
Some invalid arrangements:
[P1, D1, D2, P2] – Delivery of 2nd order is done before pickup
[P2, D1, P1, D2] – Delivery of 1st order is done before pickup
[D1, D2, P2, P1] – Delivery of both order is before pickup
方法1:
- 考虑N = 4,我们总共有8个事件。
- 提货{P1,P2,P3,P4}有4个事件,交货{D1,D2,D3,D4}有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个有效位置。
- 对于D4,我们只能将D4放在P4之后。
对于任何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 <= 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
Javascript
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)
方法二:
- 对于N个订单,我们有
- 因此,可能的安排总数为
- 现在,每个订单仅在提货后一次交货才有效。
对于每个[Pi,Di],我们无法更改此排列,即我们无法进行[Di,Pi]。每个这样的订单只有一个有效的安排。因此,我们需要为每个订单除以2。所以总的有效安排是
下面是上述方法的实现。
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)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。