当前位置:网站首页>石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
2022-07-02 10:14:00 【Joanh_Lan】
[SDOI2008] 石子合并
普通石子合并
题目描述
在一个操场上摆放着一排 N N N 堆石子。现要将石子有次序地合并成一堆。规定每次只能选相邻的 2 2 2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分。
试设计一个算法,计算出将 N N N 堆石子合并成一堆的最小得分。
输入格式
第一行一个整数 N N N。
接下来 N N N 行,第 i i i 行一个整数 a i a_i ai,代表第 i i i 堆石子的石子数。
输出格式
输出将所有石子合并为一堆的最小得分。
板子
#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();
}
环形石子合并
[NOI1995] 石子合并
题目描述
在一个圆形操场的四周摆放 N N N 堆石子,现要将石子有次序地合并成一堆,规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分。
试设计出一个算法,计算出将 N N N 堆石子合并成 1 1 1 堆的最小得分和最大得分。
输入格式
数据的第 1 1 1 行是正整数 N N N,表示有 N N N 堆石子。
第 2 2 2 行有 N N N 个整数,第 i i i 个整数 a i a_i ai 表示第 i i i 堆石子的个数。
输出格式
输出共 2 2 2 行,第 1 1 1 行为最小得分,第 2 2 2 行为最大得分。
板子
#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();
}
边栏推荐
- Astro learning notes
- Unity skframework framework (XVI), package manager development kit Manager
- Unity SKFramework框架(十五)、Singleton 单例
- [true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
- A better database client management tool than Navicat
- nohup命令
- 二、帧模式 MPLS 操作
- What are eNB, EPC and PGW?
- Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
- Halcon extract orange (Orange)
猜你喜欢

Research shows that "congenial" is more likely to become friends

Unity skframework framework (XV), singleton singleton
![[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials](/img/42/21f6d0fdd159faa8b63713624c95a2.png)
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials

What are eNB, EPC and PGW?

(POJ - 1984) navigation nightare (weighted and search set)

为什么switch 的default后面要跟break?

leetcode621. 任务调度器

OpenFOAM:lduMatrix&lduAddressing

OpenFOAM:lduMatrix&lduAddressing
![2022 zero code / low code development white paper [produced by partner cloud] with download](/img/46/92c51090e0c476df3bcffd2d11fb6d.png)
2022 zero code / low code development white paper [produced by partner cloud] with download
随机推荐
口袋奇兵点评
JS逆向之行行查data解密
mac(macos Monterey12.2 m1) 个人使用php开发
JS generates 4-digit verification code
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
MySQL: Invalid GIS data provided to function st_ geometryfromtext
OpenAPI generator: simplify the restful API development process
为什么switch 的default后面要跟break?
P3807 [template] Lucas theorem /lucas theorem
[Unity]使用GB2312,打包后程序不正常解决方案
Quantum three body problem: Landau fall
[unity] using GB2312, the solution to abnormal program after packaging
Why can't d link DLL
[cloud native database] what to do when encountering slow SQL (Part 1)?
Daily question: 1175 Prime permutation
Can automatically update the universal weekly report template, you can use it with your hand!
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
Unity skframework framework (XII), score scoring module
OpenFOAM:lduMatrix&lduAddressing
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别