dp 编程_使用动态编程(DP)的非相交和弦

dp 编程

Problem: You are given N chord and 2*N points. You have to find the number of ways in which N non- intersecting chords can be formed using these 2*N points. The answer may be too large so the answer must be taken mod with 10^9 + 7.

问题:您得到N和弦和2 * N分。 您必须找到使用这2 * N个点形成N个不相交的和弦的方式。 答案可能太大,因此答案必须为10 ^ 9 + 7。

Constraints: 1 <= N <= 100

限制条件: 1 <= N <= 100

Example:

例:

    Sample input 1:
    2
    Sample output 1:
    2

    Sample input 2:
    100
    Sample output 2:
    558488487

Explanation of the problem:

问题说明:

In the first sample provided above, there are 2 chords and 4 points. Let us label them as A, B, C, D in order. Now possible arrangements are chords AB and chords CD, the second possible arrangement is chord AD and chord BC.

在上面提供的第一个样本中,有2个和弦和4个点。 让我们依次将它们标记为A,B,C,D。 现在可能的安排是和弦AB和和弦CD,第二种可能的安排是和弦AD和和弦BC。

Note: we can’t take chord AC and chord BD because the will intersect and we only have to consider non-intersecting chord.

注意:我们不能选择和弦AC和和弦BD,因为它们将相交,我们只需要考虑不相交的和弦

Solution:

解:

Before proceeding to the solution we can see that the recursive solution will be hard to implement so we will proceed to the dynamic programming approach. In this approach, we select a point and a variable point and then make a chord using both points to divide the circle into two halves then using dp matrix we will find the number of ways to form rest of the chords in both halves multiply them and add them to the solution of ith column. We will proceed this with same fixed point and different variable point and store the answer in ith column.

在继续解决方案之前,我们可以看到递归解决方案将很难实现,因此我们将继续使用动态编程方法。 在这种方法中,我们选择一个点和一个可变点,然后使用这两个点制作一个和弦,以将圆分为两半,然后使用dp矩阵,我们发现在这两个半部中形成其余和弦的方法数量繁多,并且它们添加到的溶液 。 我们将使用相同的固定点和不同的可变点进行处理,并将答案存储在 i列中。

Algorithm:

算法:

  1. Create dp matrix in which ith cell represent the number of ways to form ith points.

    创建dp矩阵,其中第i个单元格代表形成第i个点的方式数量。

  2. Start filling dp matrix from the 4th row (no need to fill odd columns as there answer will be zero).

    开始从4填充DP矩阵第i行(无需填写奇数列,因为答案将是零)。

  3. Find the answer of each row by using dp relations.

    通过使用dp关系找到每一行的答案。

  4. If points in any of the half is 0 then add the number of ways of another half in the answer.

    如果任一半中的点为0,则在答案中加上另一半的路数。

  5. Otherwise, multiple numbers of ways in both the half and add to the answer of ith cell.

    否则,将两种方式都用多种方式加到第i 单元格的答案中。

  6. Store the answer in ith cell.

    将答案存储在 i 单元格中。

使用动态编程(DP)的不相交和弦的C ++代码 (C++ Code for Non-intersecting chords using Dynamic Programming (DP))

#include <iostream>
using namespace std;

long long int mod = 1000000007;
// function to find the number of ways
int non_intersecting(int n){
    // total number of points is twice of n
    int tot_points = 2 * n;
    // Intializing all element to zero
    long long int dp[tot_points + 1] = {0};
    // At 2 points number of ways in only 1
    dp[2] = 1;
    for(int i = 4;i<tot_points + 1;i = i + 2){
        long long int sum = 0;
        for(int j = 0;j<i;j = j + 2){
	    // number of points in first half
            int fh = j;
	    // number of points in second half
            int sh = i - j - 2;
	    // if there are 0 points in any half just 
		// add number of ways of other half
            if(fh == 0){
                sum += dp[sh];
                sum %= mod;
            }else if(sh == 0){
                sum += dp[fh];
                sum %= mod;
            }else{
		// used distributive property of modulus
                sum += ((dp[fh] * dp[sh]) % mod);
                sum %= mod;
            }
        }
        dp[i] = sum;
    } 
    return dp[tot_points];
}

// driver function 
int main() {
	int n;
	cin >> n;
	cout << n << endl;
	cout << non_intersecting(n);
	return 0;
}

Output

输出量

2
2


翻译自: https://www.includehelp.com/algorithms/non-intersecting-chords-using-dynamic-programming.aspx

dp 编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值