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.

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个顶点的正多边形,顶点编号从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 ;
}

codeforces 1140D(区间dp/思维题)的更多相关文章

  1. 又一道区间DP的题 -- P3146 [USACO16OPEN]248

    https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. 7月15日考试 题解(链表+状压DP+思维题)

    前言:蒟蒻太弱了,全打的暴力QAQ. --------------------- T1 小Z的求和 题目大意:求$\sum\limits_{i=1}^n \sum\limits_{j=i}^n kth ...

  4. CodeForces 512B(区间dp)

    D - Fox And Jumping Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  5. 状态压缩---区间dp第一题

    标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...

  6. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  7. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  8. Codeforces 1114D(区间DP)

    题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...

  9. CodeForces - 1107E 区间DP

    和紫书上的Blocks UVA - 10559几乎是同一道题,只不过是得分计算不同 不过看了半天紫书上的题才会的,当时理解不够深刻啊 不过这是一道很好区间DP题 细节看代码 #include<c ...

随机推荐

  1. jenkins忘记admin密码解决办法

    参考网址:https://www.jianshu.com/p/2995ae8157e7

  2. Mysql 索引之B+tree

    InnoDB使用的是聚簇索引,将主键组织到一棵B+树中,而行数据就储存在叶子节点上,若使用"where id = 14"这样的条件查找主键,则按照B+树的检索算法即可查找到对应的叶 ...

  3. Spring MyBatis多数据源(同包)

    创建基本的包 entity service dao 为了区分多数据源 一个用的是Mysql 一个是Oracle 方便测试, 创建MyBatis dao 映射 xml 文件 创建db.propertie ...

  4. mint修改host

    sudo xed /etc/hosts # Pycharm 0.0.0.0 account.jetbrains.com0.0.0.0 www.jetbrains.com #sublime text3 ...

  5. 【安卓进阶】Scroller理解与应用

    项目中有个需求,就是在RecyclerView的item中进行侧滑,一开始同事推荐了一个开源库,使用起来确实也方便好用,直接在布局作为父布局即可实现侧滑. 自己也非常好奇这个开源库到底用了什么API能 ...

  6. DB9针和DB25针串口的引脚定义

    <设备监控技术详解>第3章串口设备监控,本章着力介绍串口交换机和串口联网方式.本节为大家介绍标准25针串口的引脚定义. 作者:李瑞民来源:机械工业出版社 3.3 串口线的制作和转换 串口的 ...

  7. Javabean介绍

    1.javabean简介 JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公 ...

  8. 《剑指offer》总结三 之二叉树(2)

    目录 23.二叉搜索树的后序遍历序列 26.二叉搜索树与双向链表(31ms,5756k) 23.二叉搜索树的后序遍历序列 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如 ...

  9. Linux三种网络连接模式

    https://www.cnblogs.com/linjiaxin/p/6476480.html 三种模式的区别:https://www.cnblogs.com/itxiaok/p/10358055. ...

  10. Spring教程笔记(3)

    getBean() ApplicationContext接口获取Bean方法简介: • Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换: ...