当前位置:网站首页>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();
}
边栏推荐
- OpenFOAM:lduMatrix&lduAddressing
- JS reverse row query data decryption
- Answer: can the audio be set to on by default during easydss video on demand?
- 2022 zero code / low code development white paper [produced by partner cloud] with download
- Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
- Daily practice of C language --- monkeys divide peaches
- Explanation of 34 common terms on the Internet
- Pocket Raider comments
- Why can't d link DLL
- 【笔耕不辍勋章活动】生命不止,写作不息
猜你喜欢
OpenFOAM:lduMatrix&lduAddressing
uniapp小程序 subPackages分包配置
Halcon extract orange (Orange)
[cloud native database] what to do when encountering slow SQL (Part 1)?
Qt入门-制作一个简易的计算器
Redis database persistence
mac(macos Monterey12.2 m1) 个人使用php开发
Daily practice of C language --- monkeys divide peaches
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
BeanUtils--浅拷贝--实例/原理
随机推荐
Runhe hi3516 development board openharmony small system and standard system burning
JS逆向之巨量创意signature签名
【笔耕不辍勋章活动】生命不止,写作不息
Memory management 01 - link script
Dingtalk 发送消息
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
Nohup command
Can automatically update the universal weekly report template, you can use it with your hand!
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
不会看器件手册的工程师不是个好厨子
无向图的桥
TVOC, VOC, VOCs gas detection + Solution
JS逆向之行行查data解密
D如何检查null
Android kotlin broadcast technology point
What are eNB, EPC and PGW?
[Unity]使用GB2312,打包后程序不正常解决方案
Qt入门-制作一个简易的计算器
Partner cloud form strong upgrade! Pro version, more extraordinary!
de4000h存储安装配置