当前位置:网站首页>Interval DP of Changyou dynamic programming
Interval DP of Changyou dynamic programming
2022-06-27 21:50:00 【A Fei algorithm】
282. Stone merging

#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 310;
int n; // Number of piles of stones
int a[N]; // The weight of each stone
int s[N]; // The prefix and
int f[N][N]; //f[l][r] From [l,r] The cost of merging into a heap
void init()
{
memset(f, 0x3f, sizeof(f)); // Find the minimum value of interval , Initialize to maximum
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i]; // Enter the weight of each stone
s[i] = s[i - 1] + a[i]; // Find the prefix and
f[i][i] = 0; // The value of merging a stone itself is 0
}
}
int main(int argc, char const *argv[])
{
init();
for (int len = 2; len <= n; len++)// Enumeration interval length
{
for (int l = 1; l + len - 1 <= n; l++)// Interval starting point
{
int r = l + len - 1;// Interval end point
for (int k = l; k < r; k++)// enumeration
{
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
}
}
cout << f[1][n] << endl;
return 0;
}
1068. Ring stones merge

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 410;
int n; // Number of stones
int a[N]; // The mass of each stone
int s[N]; // The prefix and
int f[N][N]; //f[l][r] Express [l,r] The minimum value of interval ranges after merging into a heap
int g[N][N]; //g[l][r] Express [l,r] The maximum value of interval ranges after merging into a heap
int INF = 0x3f3f3f3f;
void init()
{
memset(f, INF, sizeof(f));
memset(g, -INF, sizeof(g));
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i + n] = a[i];
}
for (int i = 1; i <= 2 * n; i++)
{
s[i] = s[i - 1] + a[i];
f[i][i] = 0;
g[i][i] = 0;
}
}
int main(int argc, char const *argv[])
{
for (int len = 2; len <= n; len++)
{
for (int l = 1; l + len - 1 <= 2 * n; l++)
{
int r = l + len - 1;
for (int k = l; k < r; k++)
{
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
g[l][r] = max(g[l][r], f[l][k] + g[k + 1][r] + s[r] - s[l - 1]);
}
}
}
int maxv = -INF, minv = INF;
for (int i = 1; i <= n; i++)
{
minv = min(minv, f[i][i + n - 1]);
maxv = max(maxv, f[i][i + n - 1]);
}
cout << minv << maxv << endl;
return 0;
}
320. Energy necklace

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 210;
int n;
int a[N]; //a[i] It means the first one i The energy value of each bead
int f[N][N]; //f[l][r] Means to merge [l,r] The maximum energy obtained by the bead in the range
int main(int argc, char const *argv[])
{
// Process input
cin >> n; // The number of beads
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i + n] = a[i]; // Copy the interval , The circular linked list is flattened , To be linear
}
for (int len = 3; len <= n + 1; len++)
{
for (int l = 1; l + len - 1 <= 2 * n; l++)
{
int r = l + len - 1;
for (int k = l + 1; k < r; k++)
{
f[l][r] = max(f[l][r], f[l][k] + f[k][r] + a[l] * a[k] * a[r]);
}
}
}
int res = 0;
for (int i = 1; i <= n; i++)
{
res = max(res, f[i][i + n]);
}
cout << res << endl;
return 0;
}
边栏推荐
- .NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
- Go从入门到实战——共享内存并发机制(笔记)
- 空指针异常
- 抖音的兴趣电商已经碰到流量天花板?
- Codeforces Round #722 (Div. 2)
- 100 important knowledge points that SQL must master: combining where clauses
- Go从入门到实战——行为的定义和实现(笔记)
- 100 important knowledge points that SQL must master: creating calculation fields
- [LeetCode]513. 找树左下角的值
- Special training of guessing game
猜你喜欢

SQL必需掌握的100个重要知识点:过滤数据

"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer

Go from introduction to practice -- coordination mechanism (note)

Go从入门到实战——CSP并发机制(笔记)

Go从入门到实战——channel的关闭和广播(笔记)

互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?

Go从入门到实战——Context与任务取消(笔记)

∫(0→1) ln(1+x) / (x² + 1) dx

Go从入门到实战—— 多路选择和超时控制(笔记)

Bit. Store: long bear market, stable stacking products may become the main theme
随机推荐
[LeetCode]513. 找树左下角的值
Codeforces Round #721 (Div. 2)
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
SQL必需掌握的100个重要知识点:使用函数处理数据
PCIE知识点-008:PCIE switch的结构
[LeetCode]动态规划解分割数组II[Arctic Fox]
win11桌面出現“了解此圖片”如何删除
Is it safe to open an account and buy stocks? Who knows
Special training of guessing game
豆沙绿保护你的双眼
CEPH distributed storage
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
创建对象时JVM内存结构
[LeetCode]186. 翻转字符串里的单词 II
Educational Codeforces Round 108 (Rated for Div. 2)
SQL必需掌握的100个重要知识点:组合 WHERE 子句
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
专题教程——选队长游戏
Go from introduction to practice -- coordination mechanism (note)
[Sword Offer II]剑指 Offer II 029. 排序的循环链表