三角枚举基础

区间DP(2)–三角剖分

题目如下
You are given a regular polygon with nn vertices labeled from 11 to nn in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.

Calculate the minimum weight among all triangulations of the polygon.

Input

The first line contains single integer nn (3≤n≤5003≤n≤500) — the number of vertices in the regular polygon.

Output

Print one integer — the minimum weight among all triangulations of the given polygon.

Examples

input

Copy

3
output

Copy

6
input

Copy

4
output

Copy

18
Note

According to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) PP into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is PP.

In the first example the polygon is a triangle, so we don’t need to cut it further, so the answer is 1⋅2⋅3=61⋅2⋅3=6.

In the second example the polygon is a rectangle, so it should be divided into two triangles. It’s optimal to cut it using diagonal 1−31−3 so answer is 1⋅2⋅3+1⋅3⋅4=6+12=181⋅2⋅3+1⋅3⋅4=6+12=18.

大题意思就是一个n边形,你要把它切成n-1个三角形,切线不能重叠,然后计算每个三角形三条边的乘积的总和,使他最大。

实话,这个类型的题目,真的把我给弄蒙了,我想都想不到这是个DP题,更别说是区间DP。

然后,我在网上找了题解,发现有2个版本。

所以,我们对这三角剖分进行讨论

贪心算法

这个就是找规律,找出来的,证明的话有点难度

假设n=4,n=5,自己画个表,推理一下就成功了

假设n=k时,以1为顶点进行切割时是最小的,设s(k)为该值

那么n=k+1时,对于以1为切割点,就增加了k*(k+1),s(k+1)=s(k)+k*(k+1)

但是对于其他切割点,因为1不为切割点,不妨令2为切割点,那么最后多出来的是12(k+1),有人说,这不是比刚才增加的小吗,那怎么判断?所以,我们只把这个作为一个增量a,如果我们以2切割,那么势必切割三角形中会有2k(k+1),那么这个和a相加得到(2k+2)(k+1),大于k*(k+1)+(k-1)*k,这个公式是什么意思呢,我们打个比方,假如有5个点,1到5,我们增加一个点6,那么对于以1为切割点的1,4,5,6四个点的最小值小于以2为切割点的1,2,5,6四个点的最小值。那么除去这些点,剩下的点自己推理一下,切割1肯定比切割2小。

所以贪心成立,以1为切割点为最小

这个是博主龍木的贪心代码

在这里插入图片描述

经典算法

我们要像区间DP一样,要枚举所有情况。

至于为何i倒叙,j正,读者不妨都正着试一下,你会发现枚举到一些情况时,dp[i][k]和dp[k][j]没有被算出来过

为何从n-2开始呢,因为下面j要等于i+2,dp[i][j]表示的是端点i到j的最小值,自己把n置为5,模拟一边,然后再把i正序枚举一遍,就可以懂了
在这里插入图片描述

以上便是最广泛的解法了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值