codeforces 1140D(区间dp/思惟题)

D. Minimum Triangulation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.ide

Calculate the minimum weight among all triangulations of the polygon.spa

Input

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

Output

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

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.ci

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

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 131−3 so answer is 123+134=6+12=181⋅2⋅3+1⋅3⋅4=6+12=18.it

 题意:给你一个有n个顶点的正多边形,顶点编号从1-n,如今你要把这个正多边形划分为三角形,并且每一个三角形的面积都不相交,三角形的花费定义为三角形顶点编号的乘积,问花费的最少代价。io

 

思路:简单的区间dp,对于dp[i][j]的求解,咱们只要以i,j为底边,枚举顶点k(i<k<j),划分出三角形(i,j,k),而后咱们就将要求的值分为dp[i][k]+dp[k][j]+三角形(i,j,k)的花费,找到花费最小的值就能够了。class

若是敏感的话应该能够发现对于正多边形的划分,就是划分为(1,2 ,3),(1,3,4),(1,4,5)。。。。,(1,n-1,n)时它的花费是最小的,那么最后的答案就是2*3+3*4+4*5+.......+(n-1)*n。test

 

#include<cstdio> #include<algorithm>
using namespace std; const int INF=1e9; int dp[510][510]; int main(){ int n; scanf("%d",&n); for(int j=2;j<=n-1;j++){ for(int i=1;i+j<=n;i++){ dp[i][i+j]=INF;//找最小值,先初始化为无穷大 
            for(int k=i+1;k<i+j;k++){ dp[i][i+j]=min(dp[i][i+j],dp[i][k]+dp[k][i+j]+i*(i+j)*k); } } } printf("%d\n",dp[1][n]); return 0; } 
View Code
#include<cstdio> #include<algorithm>
using namespace std; const int INF=1e9; int main(){ int n; scanf("%d",&n); int ans=0; for(int i=3;i<=n;i++) ans+=i*(i-1); printf("%d\n",ans); return 0; }
View Code
相关文章
相关标签/搜索