当前位置:网站首页>Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
2022-07-02 13:45:00 【Joanh_ Lan】
[SDOI2008] Stone merging
Common stone merging
Title Description
There are rows of N N N Rubble . Now we need to combine the stones into a pile in order . It is stipulated that only the adjacent 2 2 2 Make a new pile of stones , And record the new number of stones as the score of the merger .
Try to design an algorithm , Work out what will be N N N The minimum score for a pile of stones .
Input format
The first line is an integer N N N.
Next N N N That's ok , The first i i i Line an integer a i a_i ai, On behalf of the i i i The number of stones piled .
Output format
Output the minimum score of merging all stones into a pile .
The board
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 500;
int n;
int s[N];
int dpm[N][N], dpn[N][N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> s[i],
s[i] += s[i - 1];
for (int len = 2; len <= n; len++)
{
for (int i = 1; i + len - 1 <= n; i++)
{
int l = i, r = i + len - 1;
dpm[l][r] = -1;
dpn[l][r] = 0x3f3f3f3f;
for (int k = l; k < r; k++)
{
dpm[l][r] = max(dpm[l][r], dpm[l][k] + dpm[k + 1][r] + s[r] - s[l - 1]);
dpn[l][r] = min(dpn[l][r], dpn[l][k] + dpn[k + 1][r] + s[r] - s[l - 1]);
}
}
}
cout << dpn[1][n] << '\n'
<< dpm[1][n] << '\n';
}
int main()
{
solve();
}
Ring stones merge
[NOI1995] Stone merging
Title Description
Around a circular playground N N N Rubble , Now we need to combine the stones into a pile in order , It is stipulated that only the adjacent 2 To merge into a new pile , And count the new pile of stones , Record as the score of the merger .
Try to design an algorithm , Work out what will be N N N To combine stones into 1 1 1 The minimum and maximum score of the heap .
Input format
The number of data 1 1 1 Lines are positive integers N N N, Express N N N Rubble .
The first 2 2 2 Yes N N N It's an integer , The first i i i It's an integer a i a_i ai It means the first one i i i The number of stones .
Output format
The output, 2 2 2 That's ok , The first 1 1 1 Minimum behavior score , The first 2 2 2 Maximum behavior score .
The board
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 500;
int n;
int s[N];
int dpm[N][N], dpn[N][N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> s[i],
s[i + n] = s[i];
for (int i = 1; i <= (n << 1); i++)
s[i] += s[i - 1];
for (int len = 2; len <= n; len++)
{
for (int i = 1; i + len - 1 <= (n << 1); i++)
{
int l = i, r = i + len - 1;
dpm[l][r] = -1;
dpn[l][r] = 0x3f3f3f3f;
for (int k = l; k < r; k++)
{
dpm[l][r] = max(dpm[l][r], dpm[l][k] + dpm[k + 1][r] + s[r] - s[l - 1]);
dpn[l][r] = min(dpn[l][r], dpn[l][k] + dpn[k + 1][r] + s[r] - s[l - 1]);
}
}
}
int mx = 0, mn = 0x3f3f3f3f;
for (int i = 1; i + n - 1 <= (n << 1); i++)
{
mx = max(mx, dpm[i][i + n - 1]);
mn = min(mn, dpn[i][i + n - 1]);
}
cout << mn << '\n'
<< mx << '\n';
}
int main()
{
solve();
}
边栏推荐
- Solution: Compression Technology (original version and sequel version)
- Numpy array calculation
- Qt新项目_MyNotepad++
- Pointer from entry to advanced (1)
- 记忆函数的性能优化
- 口袋奇兵点评
- 你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
- Fundamentals of machine learning (II) -- division of training set and test set
- JS reverse massive creative signature
- Verification failed, please check your call back website. You can follow the instructions
猜你喜欢

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

Gee learning notes 2

Drawing Nyquist diagram with MATLAB

Bridge of undirected graph

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

Qt入门-制作一个简易的计算器

OpenAPI generator: simplify the restful API development process

Embedded software development

Unity skframework framework (XII), score scoring module

Don't spend money, spend an hour to build your own blog website
随机推荐
Redis数据库持久化
题解:《压缩技术》(原版、续集版)
Find love for speed in F1 delta time Grand Prix
Solve "sub number integer", "jump happily", "turn on the light"
Web基础
口袋奇兵点评
【文档树、设置】字体变小
三翼鸟两周年:羽翼渐丰,腾飞指日可待
D how to check null
Explanation: here is your UFO, Goldbach conjecture
运维必备——ELK日志分析系统
TVOC, VOC, VOCs gas detection + Solution
rxjs Observable 自定义 Operator 的开发技巧
Chinese name extraction (toy code - accurate head is too small, right to play)
D为何链接不了dll
Integral link, inertia link and proportion link in Simulink
The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
最近公共祖先LCA的三种求法
代码实现MNLM
[cloud native database] what to do when encountering slow SQL (Part 1)?