HDU 1244 Max Sum Plus Plus Plus - dp
题目大意:
给一个序列,要求将序列分成m段,从左至右每一段分别长l1,l2,...lm,求最大的和是多少。
题目分析:
和最大m段子段和相似,先枚举\(i \in [1,m]\),然后$j \in [num[m], n] $,dp转移为: $$dp[j][i] = max(dp[j - 1][i], dp[j - num[i]][i - 1] + sum[j] - sum[i - num[i]])$$
code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
//#include<vector>
using namespace std;
const int N = 1005, M = 25;
int n, m, num[N];
typedef long long ll;
ll sum[N], f[N][N];
inline int read(){
ll i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar())
i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
int main(){
while(n = read()){
m = read();
sum[0] = 0;
for(int i = 1; i <= m; i++) num[i] = read();
for(int i = 1; i <= n; i++){
ll x = read() * 1LL;
sum[i] = sum[i - 1] + x;
}
int now = 0;
for(int i = 1; i <= m; i++){
now += num[i];
for(int j = now; j <= n; j++)
f[j][i] = max(f[j - 1][i], f[j - num[i]][i - 1] + sum[j] - sum[j - num[i]]);
}
wr(f[n][m]), putchar('\n');
}
}
HDU 1244 Max Sum Plus Plus Plus - dp的更多相关文章
- HDU 1244 Max Sum Plus Plus Plus
虽然这道题看起来和 HDU 1024 Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...
- HDU 1024 Max Sum Plus Plus【DP】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- HDU 1024 Max Sum Plus Plus(DP的简单优化)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- HDU 1024 Max Sum Plus Plus 简单DP
这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...
- HDU 1024 Max Sum Plus Plus(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目大意:有多组输入,每组一行整数,开头两个数字m,n,接着有n个数字.要求在这n个数字上,m块 ...
- HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...
- hdu 1003 Max Sum 最大字段和 dp
今天看了一上午dp.看不太懂啊.dp确实不简单.今天開始学习dp,搜了杭电的dp46道,慢慢来吧.白书上的写的 又不太具体,先写几道题目再说. .. 题目连接:id=516&page=1&qu ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
随机推荐
- python3输出range序列
b=range(3) #输出的是[0, 1, 2] ,其实这里如果用在循环上,代表着循环多少次,这里是循环3次.从零开始.print(list(b))
- 【Codeforces Round #431 (Div. 2) A】Odds and Ends
[链接]点击打开链接 [题意] 让你把一个数组分成奇数个部分. 且每个部分的长度都是奇数. [题解] 很简单的脑洞题. 开头和结尾一定要为奇数,然后 n为奇数的话,就选整个数组咯. n为偶数的话,不能 ...
- Android利用FTP实现与PC的上传和下载,实现二维码扫描下载
之前给老板所带的本科生课程实验所写的代码,拿出来分享一下. 下载地址: https://github.com/smartshuai/ConnectHelper.git
- 中英文对照 —— 标点符号(punctuation)
有限的几个: What Are the Fourteen Punctuation Marks in English Grammar? period:句号:comma:逗号:冒号:colon:分号:se ...
- 【2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B】
[链接]h在这里写链接 [题意] 一个硬币正面朝上的概率为q/p; 抛k次,问你偶数次朝上的概率为多少. [题解] [错的次数] 0 [反思] 在这了写反思 [代码] #include <bit ...
- jtag引脚
如果不能下载,可能原因也许是电量不足了... 在电力不足的时候,仿真也不能进行... ///////////////////////////////////////////////////////// ...
- 硬件——STM32 , SN74HC573锁存器
74HC573是一款高速CMOS器件: 上图中:输出使能为:OE 锁存使能为:LE 典型电路: 上图中:PWR-AL-0,PWR-AL-1,PWR-AL-2:是单片机输出的高低电平给573 对应的 ...
- [Node] Run Local DevDependencies from the Command Line with npx
In the past, you've needed to either write a package.json script or use the node_modules/.bin direct ...
- go 字符串 数字 整型 浮点 转换
import "strconv" //先导入strconv包 // string到int int, err := strconv.Atoi(string) // string到in ...
- Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏
在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...