2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
描述
In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth.
At the beginning, there was no mountain on the earth, only stones all over the land.
There were N piles of stones, numbered from 1 to N. Pangu wanted to merge all of them into one pile to build a great mountain. If the sum of stones of some piles was S, Pangu would need S seconds to pile them into one pile, and there would be S stones in the new pile.
Unfortunately, every time Pangu could only merge successive piles into one pile. And the number of piles he merged shouldn't be less than L or greater than R.
Pangu wanted to finish this as soon as possible.
Can you help him? If there was no solution, you should answer '0'.
输入
There are multiple test cases.
The first line of each case contains three integers N,L,R as above mentioned (2<=N<=100,2<=L<=R<=N).
The second line of each case contains N integers a1,a2 …aN (1<= ai <=1000,i= 1…N ), indicating the number of stones of pile 1, pile 2 …pile N.
The number of test cases is less than 110 and there are at most 5 test cases in which N >= 50.
输出
For each test case, you should output the minimum time(in seconds) Pangu had to take . If it was impossible for Pangu to do his job, you should output 0.
- 样例输入
-
3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4 - 样例输出
-
9
6
0题意:
n个石子堆排成一排,每次可以将连续的最少L堆,最多R堆石子合并在一起,消耗的代价为要合并的石子总数。
求合并成1堆的最小代价,如果无法做到输出0
题解:
石子归并系列题目,一般都是区间DP,于是——
dp[i][j][k] i到j 分为k堆的最小代价。显然 dp[i][j][ j-i+1]代价为0
然后[i,j] 可以划分
dp[i][j][k] = min { dp[i][d][k-1] + dp[d+1][j][1] } (k > 1&&d-i+1 >= k-1,这个条件意思就是 区间i,d之间最少要有k-1个石子)
最后合并的时候
dp[i][j][1] = min{ dp[i][d][k-1] + dp[d+1][j][1] + sum[j] - sum[i-1] } (l<=k<=r)
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
int n, l,r;
int dp[][][],sum[],a[];
//dp[i][j][t]表示区间[i,j]分为k堆的最小代价
int main()
{
while (~scanf("%d%d%d",&n,&l,&r))
{
memset(dp,0x3f,sizeof(dp));//初始化为无穷大
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];//前缀和
}
for(int i=;i<=n;i++)//初始化dp
for(int j=i;j<=n;j++)
dp[i][j][j-i+]=;//[i,j]分成j-i+1堆的代价是0
for(int len=;len<=n;len++)
{
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
for(int k=i;k<j;k++)//枚举分界点
{
for(int t=l;t<=r;t++)//堆数
dp[i][j][]=min(dp[i][j][],dp[i][k][t-]+dp[k+][j][]+sum[j]-sum[i-]);
for(int t=;t<j-i+;t++)
dp[i][j][t]=min(dp[i][j][t],dp[i][k][t-]+dp[k+][j][]);
} }
}
int ans=dp[][n][];
if(ans>=mx)
printf("0\n");
else
printf("%d\n",ans); }
}
2017北京网络赛 J Pangu and Stones 区间DP(石子归并)的更多相关文章
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- hihocoder1236(北京网络赛J):scores 分块+bitset
北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...
- icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- hihocoder 1636 : Pangu and Stones(区间dp)
Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...
- hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题
题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...
- 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset
Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...
- 2017北京网络赛 F Secret Poems 蛇形回路输出
#1632 : Secret Poems 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The Yongzheng Emperor (13 December 1678 – ...
- 2017 北京网络赛 E Cats and Fish
Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They ...
随机推荐
- Codeforces Round #600 (Div. 2) - B. Silly Mistake(模拟)
题意:有一个公司,每天有员工进出,$a[i]>0$时表示$a[i]$这个员工进入公司,$a[i]<0$时表示$-a[i]$这个员工出公司,公司对进出办公室有一些严格的规定 员工每天最多只能 ...
- WCF 数据传输SIZE过大
1.当客户端调用WCF服务时,接受数据过大,可通过以下配置解决 <basicHttpBinding> <binding name="BasicHttpBinding_Wcf ...
- vue 之 axios Vue路由与element-UI
一. 在组件中使用axios获取数据 1. 安装和配置axios 默认情况下,我们的项目中并没有对axios包的支持,所以我们需要下载安装. 在项目根目录中使用 npm安装包 npm install ...
- app生命周期之即将关闭
需求:当软件正在进行任务还未结束时,如果用户强制退出软件,需要将一些数据进行保存等处理. 策略:当用户使用多任务将软件挂起,并滑掉软件时,接下来有5妙钟的时间留给软件做处理.会调用- (void)ap ...
- Plastic Sprayer Manufacturer - Sprayer: How Does It Work?
The Plastic Sprayers Manufacturer states that a sprayer is a device that uses a basic pump me ...
- 关于java自学的内容以及感受(7.21)
直接切入正题说一下自学到的内容: 定义合法标识符的规则: 可以由英文字母,数字,_,$组成. 不能数字开头和包含空格. 不可以使用关键字和保留字,但是可以包含关键字和保留字. byte short i ...
- 新手如何配置 Chromedriver 环境变量
有一个不错的链接:https://blog.csdn.net/qq_41429288/article/details/80472064
- Java基础知识笔记第六章:接口
接口 /* 使用关键字interface来定义一个接口.接口的定义和类的定义很相似,分为接口声明和接口体 */ interface Printable{ final int max=100; void ...
- ANSYS-MFC二次开发
目录 1. 开发流程 2. 开发代码 1. 开发流程 ANSYS-MFC二次开发的思路其实是特别简单的,通常MFC主要是设计界面,然后从MFC界面中读取要设计的参数,然后根据这些设置了的参数生成APD ...
- Java 自定义DateUtils
1 /* Date d = new Date(); String s = DateUtils.DateToString(d, "yyyy-MM-dd HH:mm:ss"); Sys ...