当前位置:网站首页>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;
}
边栏推荐
- 卷积神经网络(包含代码与相应图解)
- 技术大佬准备就绪,话题C位由你决定
- [IVX junior engineer training course 10 papers to get certificates] 09 chat room production
- NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
- 成功实现边缘编码需要了解的六大经验教训
- Learning note 24 - multi sensor post fusion technology
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- 城市选择器组件实现原理
- 企业应该选择无服务器计算吗?
- The role of artificial intelligence in network security
猜你喜欢
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
matlab 使用 resample 完成重采样
What is AQS and its principle
Six lessons to be learned for the successful implementation of edge coding
Discussion on the idea of platform construction
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
Game thinking 15: thinking about the whole region and sub region Services
Volume compression, decompression
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
Three core problems of concurrent programming
随机推荐
GL Studio 5 installation and experience
KS006基于SSM实现学生成绩管理系统
new和malloc的区别
[Maya] the error of importing Maya into Metahuman
Liteos learning - first knowledge of development environment
[disease detection] realize lung cancer detection system based on BP neural network, including GUI interface
Brief description of grafana of # yyds dry goods inventory # Prometheus
Learning notes 25 - multi sensor front fusion technology
Tencent cloud techo youth dream campus trip into Wuhan University
The technology boss is ready, and the topic of position C is up to you
Since I understand the idea of dynamic planning, I have opened the door to a new world
I Brief introduction of radio energy transmission technology
error: . repo/manifests/: contains uncommitted changes
Automatically browse pinduoduo products
Introduction to ffmpeg Lib
Modeling essays series 124 a simple coding method
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Cross domain? Homology? Understand what is cross domain at once
[Floyd] post disaster reconstruction
Niuke - Huawei question bank (51~60)