Description
Given n points on a 1-D plane, where the ith point (from 0 to n-1) is at x = i, find the number of ways we can draw exactly k non-overlapping line segments such that each segment covers two or more points. The endpoints of each segment must have integral coordinates. The k line segments do not have to cover all n points, and they are allowed to share endpoints.
Return the number of ways we can draw k non-overlapping line segments. Since this number can be huge, return it modulo 109 + 7.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
- 2 <= n <= 1000
- 1 <= k <= n-1
分析
题目的意思是:这道题就是把n分成k个非重叠的片段,不要求所有的点都用于划分,即指把其中的一部分划分成K个非重叠的片段也是可以的,如例1。这道题需要用动态规划,我参考了一下别人的思路。
记 dp[i][j] 表示使用 0 … i 的点构造了 j 条线段的方案数。我们需要区分第j条线段的右端点是否就是 i,因此可以考虑把 dp[i][j]拆分成两个状态:
- dp[i][j][0] 表示第 j 条线段的右端点不是 i,也就是说我们没有办法继续延长第 j 条线段;
- dp[i][j][1] 表示第 j 条线段的右端点就是 i,也就是说我们可以选择是否继续延长第 j 条线段。
- 首先考虑 dp[i][j][0],因为第 j 条线段的右端点不是 i,因此第 i 个点没有用上,那么 0 … i-1 的点构造了 j 条线段,即
- 再考虑 dp[i][j][1],因为第 j 条线段的右端点就是 i,因此有两种情况:
- 第 j 条线段长度为 1,那么 0 … i-1 的点构造了 j-1 条线段,即
- 第 j 条线段长度大于 1,那么删去第 j 条线段 i-1 … i 的这一部分,0 … i-1 的点仍然构造了 j 条线段,并且点 i-1 是属于第 j 条线段的,即
- 加上边界条件 dp[0][0][0] = 1,最终答案即为 dp[n-1][k][0] + dp[n−1][k][1]
代码
参考文献