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 ...
随机推荐
- kali虚拟机添加共享文件夹
1.保证安装了vmtools 2.在虚拟机本身设置共享文件夹,如图 其中选的这个文件夹就是宿主机里待共享的文件夹. 3,在kali里启用它: vmhgfs-fuse .host:[宿主机文件夹] /m ...
- acm:屁屁上的巴掌
涉及算法:深度搜索 题目: 题目描述 小新是个调皮的孩子,他总是会把衣服搞脏,他的妈妈美伢非常的生气,于是在<和妈妈的约定条款>加上了第三百七十七条:小新衣服上每有一块污渍妈妈就会打小新的 ...
- C#File类常用文件操作以及一个模拟的控制台文件管理系统
重温一下C#中File类的一些基本操作: File类,是一个静态类,主要是来提供一些函数库用的. 使用时需要引入System.IO命名空间. 一.常用操作: 1.创建文件方法 //参数1:要创建的文件 ...
- Tomcat性能优化(转载)
出处:微信订阅号GitChat精品课程 — Tomcat性能优化 Tomcat 简单介绍 Sun 公司创建了第一个 Servlet 容器,即 Java Web Server,但 JWS 只是为了演示 ...
- js switch 字符串
//字符串var day="dd";switch (day){case "dd": x="day it's dd"; break;case ...
- git 执行pull错误如何撤销
比如你当前所在的空间是trunk,但是执行了git pull origin branches,这时需要回滚回去,可以用一下步骤: 1.运行git reflog命令查看你的历史变更记录,如下: 69fd ...
- F1赛道 - Bahrain International Circuit | 巴林国际赛道
刚看完F1巴林站比赛,23点到1点,整整两个小时,比赛相当精彩. 从排位赛结果看,法拉利似乎肯定包揽1-2名,可惜天公不作美,今晚风大.沙多:vettel自己失误,鼻翼掉了,还在被汉密尔顿超车的时候自 ...
- 安装和启动json-server
安装json-server JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源 npm i -g json-server ...
- 【转】解决Android 6.0 NoSuchContextException 和WEBVIEW_undefined 的问题
在 Android 4.4 操作系统上测试混合应用时,可以直接顺利的在native模式和webview模式之间切换,但是在Android6.0 操作系统上却报NoSuchContextExceptio ...
- 分别用Excel和python进行日期格式转换成时间戳格式
最近在处理一份驾驶行为方面的数据,其中要用到时间戳,因此就在此与大家一同分享学习一下. 1.什么是时间戳? 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01 ...