
2 seconds
256 megabytes
standard input
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.
Calculate the minimum weight among all triangulations of the polygon.
The first line contains single integer nn (3≤n≤5003≤n≤500) — the number of vertices in the regular polygon.
Print one integer — the minimum weight among all triangulations of the given polygon.
3
6
4
18
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个顶点的正多边形,顶点编号从1-n,现在你要把这个正多边形划分为三角形,而且每个三角形的面积都不相交,三角形的花费定义为三角形顶点编号的乘积,问花费的最少代价。
思路:简单的区间dp,对于dp[i][j]的求解,我们只要以i,j为底边,枚举顶点k(i<k<j),划分出三角形(i,j,k),然后我们就将要求的值分为dp[i][k]+dp[k][j]+三角形(i,j,k)的花费,找到花费最小的值就可以了。
如果敏感的话应该可以发现对于正多边形的划分,就是划分为(1,2 ,3),(1,3,4),(1,4,5)。。。。,(1,n-1,n)时它的花费是最小的,那么最后的答案就是2*3+3*4+4*5+.......+(n-1)*n。
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=1e9;
int dp[][];
int main(){
int n;
scanf("%d",&n);
for(int j=;j<=n-;j++){
for(int i=;i+j<=n;i++){
dp[i][i+j]=INF;//找最小值,先初始化为无穷大
for(int k=i+;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[][n]);
return ;
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=1e9; int main(){
int n;
scanf("%d",&n);
int ans=;
for(int i=;i<=n;i++)
ans+=i*(i-);
printf("%d\n",ans);
return ;
}