当前位置:网站首页>石子合并板子【区间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();
}
边栏推荐
- Three talking about exception -- error handling
- Can automatically update the universal weekly report template, you can use it with your hand!
- Find love for speed in F1 delta time Grand Prix
- Achievements in science and Technology (27)
- nohup命令
- [indomitable medal activity] life goes on and writing goes on
- Word efficiency guide - word's own template
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
- SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
猜你喜欢
leetcode621. task scheduler
Essential for operation and maintenance - Elk log analysis system
[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
Engineers who can't read device manuals are not good cooks
Explanation: here is your UFO, Goldbach conjecture
Student course selection information management system based on ssm+jsp framework [source code + database]
Unity skframework framework (XVI), package manager development kit Manager
Node. JS accessing PostgreSQL database through ODBC
Unity skframework framework (XIII), question module
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
随机推荐
Lucky numbers in the [leetcode daily question] matrix
Web Foundation
D为何链接不了dll
Partner cloud form strong upgrade! Pro version, more extraordinary!
What are eNB, EPC and PGW?
最近公共祖先LCA的三种求法
Unity skframework framework (XIX), POI points of interest / information points
How to modify the error of easydss on demand service sharing time?
P3807 [template] Lucas theorem /lucas theorem
2022零代码/低代码开发白皮书【伙伴云出品】附下载
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
de4000h存储安装配置
能自动更新的万能周报模板,有手就会用!
[Blue Bridge Cup] children's worship circle
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
OpenApi-Generator:简化RESTful API开发流程
【云原生数据库】遇到慢SQL该怎么办(上)?
Unity skframework Framework (XVI), package manager Development Kit Manager
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
题解《子数整数》、《欢乐地跳》、《开灯》