当前位置:网站首页>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
边栏推荐
- Teach you how to build the ruoyi system by Tencent cloud
- 拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
- JS determines whether the numeric string contains characters
- New terminal play method: script guidance independent of technology stack
- Alibaba tip: it is better to create threads manually
- Other problems encountered in debugging fingerprints
- Practice and exploration of knowledge map visualization technology in meituan
- Painless upgrade of pixel series
- DIY is an excel version of subnet calculator
- Innovation training (VI) routing
猜你喜欢
Spark optimization
Unity RawImage背景无缝连接移动
Simply drag objects to the item bar
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
[WinUI3]编写一个仿Explorer文件管理器
DIY 一个 Excel 版的子网计算器
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
深度学习笔记 —— 语义分割和数据集
Introduction to raspberry pie 3B - system installation
Innovation training (IX) integration
随机推荐
Getprop property
The programmer starts the required application with one click of window bat
Graduation project
Learning Android from scratch -- Introduction
[database] MySQL multi table query (I)
The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
Wechat payment function
PIP3 installation requests Library - the most complete pit sorting
深度学习笔记 —— 数据增广
PHP counts the number of files in the specified folder
Spark case - wordcount
Innovation training (VII) FBV view & CBV view
Unity摄像头跟随鼠标旋转
JS determines whether the numeric string contains characters
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Special topic of data intensive application system design
Leetcode 1547: minimum cost of cutting sticks
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
[database] MySQL basic operation (basic operation ~)
信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮