当前位置:网站首页>1069. Division of convex polygons (thinking, interval DP)
1069. Division of convex polygons (thinking, interval DP)
2022-07-02 01:45:00 【seez】

Limiting conditions :
- It is divided into N-2 Triangles
- Disjoint
analysis :

Because a triangle needs three vertices , We can enumerate three vertices first , Then observe the nature
Pictured , When we show i,j,k Three vertices , At present, it can be divided into three sets
- Polygon set on the left + The polygon set on the right + Current triangle
It looks like an energy Necklace , By enumerating three points , Then calculate the set , Because we need to calculate the set of all individual triangles first then And then recurs to the weight of a whole polygon ( Enumerate two endpoints , At least one point in the middle , therefore len by 3, To ensure at least one point in the middle )
therefore len At most from 3 Start enumeration , Because of n vertices , at most n Edge shape , So enumerate to n,3<=len<=n
We found that , Because different triangles cannot intersect , So for a polygon set

State means : All by (i,i+1),(i+1,i+2)...(j-1,j) A collection of triangles divided by polygons
subset division / State calculation : At present dp[i][j] It can be divided into three subsets = Polygon set on the left + Current triangle + Set of polygons on the right ,dp[i][j]=dp[i][k]+dp[k][j]+w[i]*w[i]*w[k], According to the break point k Can be divided into multiple subsets
- choice i+1 As a breakpoint
- ...
- choice j-1 As a breakpoint
Recursive order :3<=len<=n
initialization : infinity
The border :dp[i][i]=dp[i][i+1]=0
Due to the need for high accuracy , So here we use vector Use high precision
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 55;
vector<int> dp[N][N];
int w[N];
typedef long long ll;
vector<int> init(ll a)
{
if (a == (long long)1e18)
{
vector<int> res(35, 0);
res.push_back(1);
reverse(res.begin(), res.end());
return res;
}
vector<int> res;
while (a)
{
res.push_back(a % 10);
a /= 10;
}
reverse(res.begin(), res.end());
return res;
}
vector<int> add(vector<int> a, vector<int> b)
{
vector<int> c;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int t = 0;
for (int i = 0;i < a.size() || i < b.size();i++)
{
if (i < a.size())
t += a[i];
if (i < b.size())
t += b[i];
c.push_back(t % 10);
t /= 10;
}
if (t)
c.push_back(t);
reverse(c.begin(), c.end());
return c;
}
vector<int> mul(vector<int> a, ll r)
{
vector<int> c;
reverse(a.begin(), a.end());
ll t = 0;
for (int i = 0;i < a.size() || t;i++)
{
if (i < a.size())
t += a[i] * r;
c.push_back(t % 10);
t /= 10;
}
while (c.back() == 0 && c.size() > 1) c.pop_back();
reverse(c.begin(), c.end());
return c;
}
bool cmp(vector<int>& a, vector<int>& b)
{
if (a.size() != b.size())
return a.size() < b.size();
else
for (int i = 0;i < a.size();i++)
if (a[i] != b[i])
return a[i] < b[i];
return false;
}
int main()
{
int n;
cin >> n;
for (int i = 1;i <= n;i++)
cin >> w[i];
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
dp[i][j] = init((long long)1e18);
for (int len = 1;len <= n;len++)
for (int i = 1;i + len - 1 <= n;i++)
{
int j = i + len - 1;
if (len == 1 || len == 2) dp[i][j] = init(0);
else
for (int k = i + 1;k < j;k++)
{
auto temp = add(dp[i][k], dp[k][j]);
auto t = mul(mul(init(w[i]), w[j]), w[k]);
temp = add(temp, t);
if (cmp(temp,dp[i][j]))
dp[i][j] = temp;
}
}
for (auto t : dp[1][n])
cout << t;
return 0;
}边栏推荐
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
- [IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
- Ks006 student achievement management system based on SSM
- 如何用一款产品推动「品牌的惊险一跃」?
- Docker installing Oracle_ 11g
- Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- Using tabbar in wechat applet
- ES6 new method of string
- Automatically browse pinduoduo products
猜你喜欢

6-3 vulnerability exploitation SSH environment construction

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

k线图形态这样记(口诀篇)

Raspberry pie 4B learning notes - IO communication (1-wire)

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?

Four basic strategies for migrating cloud computing workloads

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories

Three core problems of concurrent programming

Number of palindromes in C language (leetcode)
![Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation](/img/eb/b1382428d6578b8561d7fcc1a2a5cd.jpg)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
随机推荐
城市选择器组件实现原理
6-3漏洞利用-SSH环境搭建
It's already 30. Can you learn programming from scratch?
牛客网——华为题库(51~60)
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Data visualization in medical and healthcare applications
Basic concepts of machine learning
Memorabilia of domestic database in June 2022
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
三分钟学会基础k线图知识
Architecture evolution from MVC to DDD
Brief introduction to the development of mobile network
Raspberry pie 4B learning notes - IO communication (1-wire)
人工智能在网络安全中的作用
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
Failed to transform file 'xxx' to match attributes
Four basic strategies for migrating cloud computing workloads
机器学习基本概念
[IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games