给定两个阵列L []和大小的[R [] N,和整数K,任务是找到的路的数目来选择确切ķ不相交的范围通过取元件形成呈现从排列L相同的索引为[]和R[]。
例子:
Input: N = 7, K = 3, L[] = {1, 3, 4, 6, 1, 5, 8}, R[] = {7, 8, 5, 7, 3, 10, 9}
Output: 9
Explanation:
The possible ways of selecting K ranges are:
- Select ranges {1, 7}, {3, 8}, and {4, 5}
- Select ranges {1, 7}, {3, 8}, and {6, 7}
- Select ranges {1, 7}, {3, 8}, and {1, 3}
- Select ranges {1, 7}, {3, 8}, and {5, 10}
- Select ranges {1, 7}, {4, 5}, and {5, 10}
- Select ranges {1, 7}, {6, 7}, and {5, 10}
- Select ranges {3, 8}, {4, 5}, and {5, 10}
- Select ranges {3, 8}, {6, 7}, and {5, 10}
- Select ranges {3, 8}, {5, 10}, and {8, 9}
Input: N = 2, K = 2, L[] = {100, 200}, R[] ={ 201, 300}
Output: 0
朴素的方法:解决问题的最简单的方法是选择每个可能的不同K对,并检查它们对于所有范围的每一对是否不相交。
时间复杂度: O(N!)
辅助空间: O(1)
有效的方法:可以通过检查每个范围可以与当前范围一起使用的非不相交范围的数量来优化上述方法。按照以下步骤优化上述方法:
- 初始化一个变量,比如, cnt来计算每个当前范围的非不相交范围的数量。
- 初始化一个成对向量,假设预处理后将所有左边界存储为{L[i], 1} ,将右边界存储为范围的{R[i]+1, -1} 。
- 使用变量i迭代范围[0, N] ,并执行以下步骤:
- 将{L[i], 1}和{R[i]+1, -1} 对推入预处理过的向量中。
- 对向量进行排序,以非递减顺序进行预处理。
- 初始化变量,例如ans和cnt为0以存储答案并存储与当前范围相交的段。
- 迭代使用变量i预处理的向量并执行以下操作:
- 如果该对的第二个元素为1且cnt >= K-1,则将ans增加cnt C K-1并将cnt更新为cnt+1。
- 否则,将cnt更新为cnt+1。
- 最后,完成上述步骤后,打印ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Utility function to calculate nCr
int nCr(int n, int r, int* f)
{
return f[n] / f[r] * f[n - r];
}
// Function to calculate number of ways
// to choose K ranges such that no two of
// them are disjoint.
int NumberOfWaysToChooseKRanges(int L[], int R[],
int N, int K)
{
// Stores the factorials
int f[N + 1];
f[0] = 1;
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
f[i] = f[i - 1] * i;
}
// Preprocessing the ranges into
// new vector
vector > preprocessed;
// Traverse the given ranges
for (int i = 0; i < N; i++) {
preprocessed.push_back(make_pair(L[i], 1));
preprocessed.push_back(make_pair(R[i] + 1, -1));
}
// Sorring the preprocessed vector
sort(preprocessed.begin(), preprocessed.end());
// Stores the result
int ans = 0;
// Stores the count of non-disjoint ranges
int Cnt = 0;
// Traverse the proeprocesse vector of pairs
for (int i = 0; i < preprocessed.size(); i++) {
// If current point is a left boundary
if (preprocessed[i].second == 1) {
if (Cnt >= K - 1) {
// Update the answer
ans += nCr(Cnt, K - 1, f);
}
// Increment cnt by 1
Cnt++;
}
else {
// Decrement cnt by 1
Cnt--;
}
}
// Return the ans
return ans;
}
// Driver Code
int main()
{
// Given Input
int N = 7, K = 3;
int L[] = { 1, 3, 4, 6, 1, 5, 8 };
int R[] = { 7, 8, 5, 7, 3, 10, 9 };
// Function Call
cout << NumberOfWaysToChooseKRanges(L, R, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Utility function to calculate nCr
static int nCr(int n, int r, int f[])
{
return f[n] / f[r] * f[n - r];
}
// Function to calculate number of ways
// to choose K ranges such that no two of
// them are disjoint.
static int NumberOfWaysToChooseKRanges(int L[], int R[],
int N, int K)
{
// Stores the factorials
int f[] = new int[N + 1];
f[0] = 1;
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
f[i] = f[i - 1] * i;
}
// Preprocessing the ranges into
// new vector
Vector preprocessed = new Vector();
// Traverse the given ranges
for (int i = 0; i < N; i++) {
preprocessed.add(new pair(L[i], 1));
preprocessed.add(new pair(R[i] + 1, -1));
}
// Sorting the preprocessed vector
Collections.sort(preprocessed, new Comparator() {
@Override public int compare(pair p1, pair p2)
{
if (p1.first != p2.first)
return (p1.first - p2.first);
return p1.second - p2.second;
}
});
// Stores the result
int ans = 0;
// Stores the count of non-disjoint ranges
int Cnt = 0;
// Traverse the proeprocesse vector of pairs
for (int i = 0; i < preprocessed.size(); i++) {
// If current point is a left boundary
if (preprocessed.elementAt(i).second == 1) {
if (Cnt >= K - 1) {
// Update the answer
ans += nCr(Cnt, K - 1, f);
}
// Increment cnt by 1
Cnt++;
}
else {
// Decrement cnt by 1
Cnt--;
}
}
// Return the ans
return ans;
}
// Driver Code
public static void main (String[] args) {
// Given Input
int N = 7, K = 3;
int L[] = { 1, 3, 4, 6, 1, 5, 8 };
int R[] = { 7, 8, 5, 7, 3, 10, 9 };
// Function Call
System.out.println(NumberOfWaysToChooseKRanges(L, R, N, K));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Utility function to calculate nCr
def nCr(n, r, f):
return f[n] // f[r] * f[n - r]
# Function to calculate number of ways
# to choose K ranges such that no two of
# them are disjoint.
def NumberOfWaysToChooseKRanges(L, R, N, K):
# Stores the factorials
f = [0 for i in range(N + 1)]
f[0] = 1
# Iterate over the range [1, N]
for i in range(1, N + 1, 1):
f[i] = f[i - 1] * i
# Preprocessing the ranges into
# new vector
preprocessed = []
# Traverse the given ranges
for i in range(N):
preprocessed.append([L[i], 1])
preprocessed.append([R[i] + 1, -1])
# Sorring the preprocessed vector
preprocessed.sort()
# Stores the result
ans = 0
# Stores the count of non-disjoint ranges
Cnt = 0
# Traverse the proeprocesse vector of pairs
for i in range(len(preprocessed)):
# If current point is a left boundary
if (preprocessed[i][1] == 1):
if (Cnt >= K - 1):
# Update the answer
ans += nCr(Cnt, K - 1, f)
# Increment cnt by 1
Cnt += 1
else:
# Decrement cnt by 1
Cnt -= 1
# Return the ans
return ans
# Driver Code
if __name__ == '__main__':
# Given Input
N = 7
K = 3
L = [ 1, 3, 4, 6, 1, 5, 8 ]
R = [ 7, 8, 5, 7, 3, 10, 9 ]
# Function Call
print(NumberOfWaysToChooseKRanges(L, R, N, K))
# This code is contributed by SURENDRA_GANGWAR
Javascript
输出
9
时间复杂度: O(N*log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。