codeforces 1140D(区间dp/思维题)
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 ;
}
codeforces 1140D(区间dp/思维题)的更多相关文章
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- 7月15日考试 题解(链表+状压DP+思维题)
前言:蒟蒻太弱了,全打的暴力QAQ. --------------------- T1 小Z的求和 题目大意:求$\sum\limits_{i=1}^n \sum\limits_{j=i}^n kth ...
- CodeForces 512B(区间dp)
D - Fox And Jumping Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- 状态压缩---区间dp第一题
标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...
- Codeforces 1114D(区间DP)
题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...
- CodeForces - 1107E 区间DP
和紫书上的Blocks UVA - 10559几乎是同一道题,只不过是得分计算不同 不过看了半天紫书上的题才会的,当时理解不够深刻啊 不过这是一道很好区间DP题 细节看代码 #include<c ...
随机推荐
- 承接VR外包,虚拟现实外包,北京正规公司
我们制作各类型VR全景虚拟现实,增强现实视频制作.录制等项目.品质保证,售后完备,可签合同.contectus: 13911652504(技术经理tommy) 承揽VR外包 虚拟现实外包 U3D外包( ...
- 一个可以配置阴影方向和颜色的类 CardView 控件 SCardView
一.控件简述 今天给大家推荐一个控件 SCardView ,看名字就很容易才出来它其实就是一个 CardView .把它拿出来,是因为它解决了一些 CardView 无法实现的需求以及简化了 Card ...
- R语言 重命名目录下所有文件
myfilepath <- "F:/paper2/climateExposure/wjj_mec/second/paths/" setwd(myfilepath) allty ...
- Dart学习-操作符
dart定义了下表所示的运算符.你可以重写许多这些运算符. 描述 运算符 一元后缀 expr++ expr-- () [] . ?. 一元前缀 -expr !expr ~expr ++expr --e ...
- 开启BBR
BBR 目的是要尽量跑满带宽, 并且尽量不要有排队的情况, 效果并不比速锐差Linux kernel 4.9+ 已支持 tcp_bbr 下面简单讲述基于KVM架构VPS如何开启附:OpenVZ 架构V ...
- ECharts前端图形展示
这次负责慢查询预警,前后端都是自己处理,这次遇到了前端作图的需求,做一个记录以便后续使用: 使用的作图方式是ECharts,相应的example官方有相应的文档和使用方法,比较简单,一下只贴链接: h ...
- python-json函数
json函数使用 JSON 函数需要导入 json 库:import jsonjson函数包含:json.dumps,json.loads,json.load,json.dump #1.json.du ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence
import urllib.request def load_baidu(): url = "https://www.baidu.com/" header = {"Use ...
- Objective-C基础教程 笔记
一.对C的扩展 1. #import VS #include C语言使用#include语句通知编译器应在头文件中查询定义. OC中也可以使用#include,但几乎不这么用,而是使用#import. ...
- sqlserverdatasouce控件如何让添加删除修改自动化
对于sqlserverdatasouce控件,添加插入修改和删除命令,可以自动删除修改更新数据,不需要编写一行代码,但是有时更新失败,原因在于选中了[开放式并发],这个选中,如果该表与其他的数据表关联 ...