当前位置:网站首页>Harmonious dormitory (linear DP / interval DP)
Harmonious dormitory (linear DP / interval DP)
2022-04-23 04:56:00 【to cling】
Solution One ( linear dp)
linear dp
- State means : d p [ i ] [ j ] surface in front i Zhang do product use fall j individual wood plate Of most Small Noodles product dp[i][j] Before presentation i Zhang's work is used up j The minimum area of a board dp[i][j] surface in front i Zhang do product use fall j individual wood plate Of most Small Noodles product
- State shift : d p [ i ] [ j ] = m i n ( d p [ k − 1 ] [ j − 1 ] + ( i − k + 1 ) ∗ m a x ( H [ k , i ] ) ) ( i ≤ k ≤ j ) dp[i][j] = min(dp[k - 1][j - 1] + (i - k + 1) * max(H[k, i])) ~~~( i \leq k \leq j) dp[i][j]=min(dp[k−1][j−1]+(i−k+1)∗max(H[k,i])) (i≤k≤j)
Enumerate the starting position of the last board k,[1, k - 1] use j - 1 A board , [k, i] Use a piece .
Code One
int a[102];// Work height
int f[102][102];// Interval maximum
int dp[102][102];
int main()
{
IOS;
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
for (int k = i; k <= j; k++)
f[i][j] = max(f[i][j], a[k]);
// State initialization
mt(dp, 0x3f);
for (int i = 0; i <= n; i++)
dp[i][1] = i * f[1][i];
for (int i = 1; i <= n; i++)// Stage
for (int j = 2; j <= m; j++)// state
for (int k = j; k <= i; k++)// Decision making
dp[i][j] = min(dp[i][j], dp[k - 1][j - 1] + f[k][i] * (i - k + 1));
cout << dp[n][m] << endl;
return 0;
}
Solution Two
Section dp
- State means : d p [ l ] [ r ] [ c n t ] surface in The first i Zhang do product To The first j Zhang do product use fall c n t individual plate Son Of most Small Noodles product dp[l][r][cnt] It means the first one i Zhang's work goes to the j Zhang's work is used up cnt The minimum area of a board dp[l][r][cnt] surface in The first i Zhang do product To The first j Zhang do product use fall cnt individual plate Son Of most Small Noodles product
- State shift : d p [ l ] [ r ] [ c n t ] = m i n ( d p [ l ] [ i ] [ j ] + d p [ i + 1 ] [ r ] [ c n t − j ] ) dp[l][r][cnt] = min(dp[l][i][j] + dp[i + 1][r][cnt - j]) dp[l][r][cnt]=min(dp[l][i][j]+dp[i+1][r][cnt−j])
[ l , i ] use j individual plate Son , [ i + 1 ] [ r ] use c n t − j individual plate Son [l, i] use j A board , [i + 1][r] use cnt - j A board [l,i] use j individual plate Son ,[i+1][r] use cnt−j individual plate Son
Code Two
int a[102];
int f[102][102];
int dp[102][102][102];
int n, m;
int dfs(int l, int r, int cnt)
{
if (l > r || r - l + 1 < cnt) return inf;
if (cnt == 0) return inf;// There's no board
if (cnt == 1) return dp[l][r][1] = f[l][r] * (r - l + 1);
if (dp[l][r][cnt] != inf) return dp[l][r][cnt];
for (int i = l; i < r; i++)
for (int j = 1; j < cnt; j++)
dp[l][r][cnt] = min(dp[l][r][cnt], dfs(l, i, j) + dfs(i + 1, r, cnt - j));
return dp[l][r][cnt];
}
int main()
{
IOS;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
mt(dp, 0x3f);
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
for (int k = i; k <= j; k++)
f[i][j] = max(f[i][j], a[k]);
cout << dfs(1, n, m);
return 0;
}
版权声明
本文为[to cling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230455049867.html
边栏推荐
- [WinUI3]编写一个仿Explorer文件管理器
- How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
- 多线程基本概念(并发与并行、线程与进程)和入门案例
- Mac 进入mysql终端命令
- 信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
- Spark small case - RDD, broadcast
- AQS source code reading
- Teach you how to build the ruoyi system by Tencent cloud
- View, modify and delete [database] table
- 【数据库】表的查看、修改和删除
猜你喜欢
JS engine loop mechanism: synchronous, asynchronous, event loop
Pixel mobile phone brick rescue tutorial
Record the ThreadPoolExecutor main thread waiting for sub threads
Learning Android from scratch -- Introduction
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Spark optimization
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
View analysis of scenic spots in ArcGIS
[2021] Spatio-Temporal Graph Contrastive Learning
redis数据类型有哪些
随机推荐
[database] MySQL basic operation (basic operation ~)
L2-011 玩转二叉树(建树+BFS)
PHP 统计指定文件夹下文件的数量
PHP counts the number of files in the specified folder
Use AES encryption - reuse the wisdom of predecessors
Wine (COM) - basic concept
Progress of innovation training (III)
多线程基本概念(并发与并行、线程与进程)和入门案例
Practice and exploration of knowledge map visualization technology in meituan
【数据库】MySQL单表查询
Learning Android from scratch -- baseactivity and activitycollector
信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
[database] MySQL single table query
Unity摄像头跟随鼠标旋转
L2-011 play binary tree (build tree + BFS)
Better way to read configuration files than properties
The unity camera rotates with the mouse