当前位置:网站首页>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;
}
边栏推荐
- Knowledge sorting of exception handling
- How to participate in openharmony code contribution
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- Go從入門到實戰——接口(筆記)
- excel读取文件内容方法
- win11桌面出現“了解此圖片”如何删除
- GBase 8a OLAP分析函数cume_dist的使用样例
- Go from introduction to practice -- coordination mechanism (note)
- Flask----应用案例
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
猜你喜欢

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

图解基于AQS队列实现的CountDownLatch和CyclicBarrier

Go from entry to practice -- CSP concurrency mechanism (note)

让马化腾失望了!Web3.0,毫无希望

洛谷P5706 再分肥宅水

Knowledge sorting of exception handling

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

Tiktok's interest in e-commerce has hit the traffic ceiling?

我想我要开始写我自己的博客了。

.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
随机推荐
Oracle migration MySQL unique index case insensitive don't be afraid
Null pointer exception
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
DO280OpenShift访问控制--security policy和章节实验
ABC-Teleporter Setting-(思维+最短路)
快速excel导出
畅游动态规划之区间DP
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
Go从入门到实战——Context与任务取消(笔记)
CEPH distributed storage
Go from introduction to actual combat - context and task cancellation (notes)
Special training of guessing game
Let Ma Huateng down! Web3.0, hopeless
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
Process control task
Acwing周赛57-数字操作-(思维+分解质因数)
100 important knowledge points that SQL must master: retrieving data
win11桌面出现“了解此图片”如何删除
专题教程——选队长游戏
Go from entry to practice - multiple selection and timeout control (notes)