当前位置:网站首页>石子合并板子【区间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();
}
边栏推荐
- On flow delivery between microservices
- Engineers who can't read device manuals are not good cooks
- 二、帧模式 MPLS 操作
- Lucky numbers in the [leetcode daily question] matrix
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- Mysql常用命令详细大全
- Unity skframework framework (XIV), extension extension function
- Memory management 01 - link script
- Bridge of undirected graph
- D为何链接不了dll
猜你喜欢

Unity SKFramework框架(十三)、Question 问题模块
![[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

Three methods of finding LCA of the nearest common ancestor

Why is the default of switch followed by break?

解答:EasyDSS视频点播时音频是否可以设置为默认开启?
![Lucky numbers in the [leetcode daily question] matrix](/img/c8/47a22644bf8cc1f49c5668d72161b6.jpg)
Lucky numbers in the [leetcode daily question] matrix

OpenFOAM:lduMatrix&lduAddressing

What are eNB, EPC and PGW?

Unity SKFramework框架(十六)、Package Manager 开发工具包管理器

How to modify the error of easydss on demand service sharing time?
随机推荐
mac(macos Monterey12.2 m1) 个人使用php开发
Node. JS accessing PostgreSQL database through ODBC
Web基础
Fundamentals of machine learning (II) -- division of training set and test set
Unity SKFramework框架(二十)、VFX Lab 特效库
What are eNB, EPC and PGW?
Download files and preview pictures
Unity SKFramework框架(十五)、Singleton 单例
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Performance optimization of memory function
What are eNB, EPC and PGW?
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
题解:《压缩技术》(原版、续集版)
Add sequence number column to query results in MySQL
[youcans' image processing learning course] general contents
三翼鸟两周年:羽翼渐丰,腾飞指日可待
【youcans 的图像处理学习课】总目录
Pointer from entry to advanced (1)
Bridge of undirected graph
每日一题:1175.质数排列