当前位置:网站首页>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;
}
边栏推荐
- 流程控制任务
- TypeScript学习
- Null pointer exception
- Go from entry to practice - dependency management (notes)
- ARCS模型介绍
- Special training of guessing game
- List of language weaknesses --cwe, a website worth learning
- Codeforces Global Round 14
- Go from introduction to practice - Interface (notes)
- Codeforces Round #719 (Div. 3)
猜你喜欢

How to participate in openharmony code contribution

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

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

Codeforces Round #717 (Div. 2)

PCIE知识点-008:PCIE switch的结构

Knowledge sorting of exception handling

STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app

win11桌面出现“了解此图片”如何删除

创建对象时JVM内存结构
随机推荐
Little known MySQL import data
Special training of guessing game
猜拳游戏专题训练
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
Go从入门到实战——仅执行一次(笔记)
Go from introduction to practice - polymorphism (note)
Go从入门到实战——依赖管理(笔记)
Day8 ---- 云资讯项目介绍与创建
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
Codeforces Round #722 (Div. 2)
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
qt 大文件生成md5校验码
∫(0→1) ln(1+x) / (x ² + 1) dx
OpenSSL 编程 一:基本概念
TreeSet details
100 important knowledge points that SQL must master: filtering data
GBase 8a OLAP函数group by grouping sets的使用样例
Go从入门到实战——协程机制(笔记)