HDU1024 多段最大和 DP
题目大意:
在n个数,求不重复的m段中的数据总和的最大值
令dp[i][j]表示将前j个数分成 i 段时得到的最大值(必取到第 j 个数)
状态转移可列为 dp[i][j]=Max(dp[i][j-1]+a[j] , Max( dp[i-1][k] ) + a[j] ) 0<k<j
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std; const int N = ;
int a[N] , dp[N] , ans[N]; int main()
{
int n , m;
while(scanf("%d%d" , &m , &n) == )
{
memset(dp , , sizeof(dp));
memset(ans , , sizeof(ans));
for(int i= ; i<=n ; i++){
scanf("%d" , a+i);
}
int maxn = -0x7fffffff;
for(int i= ; i<=m ; i++){
maxn = -0x7fffffff;
for(int j=i ; j<=n ; j++){
dp[j] = max(dp[j-] + a[j] , ans[j-] + a[j]);
ans[j-] = maxn;
maxn = max(dp[j] , maxn);
}
}
printf("%d\n" , maxn);
}
return ;
}
HDU1024 多段最大和 DP的更多相关文章
- HDU 1024 (不重叠m段最大和) Max Sum Plus Plus
题解是看的这里的: http://www.acmerblog.com/hdu-1024-Max-Sum-Plus-Plus-1276.html 当前这个状态是dp[i][j],i 表示当前的段,j表示 ...
- P1121 环状最大两段子段和(DP)
P1121 环状最大两段子段和 难度 提高+/省选- 题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件 ...
- CF833B-线段树优化DP
CF833B-线段树优化DP 题意 将一个长为\(n\)的序列分成\(k\)段,每段贡献为其中不同数字的个数,求最大贡献和. 思路 此处感谢@gxy001 聚铑的精彩讲解 先考虑暴力DP,可以想到一个 ...
- HDU1024Max Sum Plus Plus(M段最大和)
题意:求一个数组中 M 段的 最大和 没看明白怎么搞得 抽空来看,写的不赖 #include <iostream> #include <cstring> #include &l ...
- 多段图动态规划dp
多段图问题是DP的基础题目.大体的意思是有一个赋权有向图,其顶点集被分为几个子集.求经过每个子集从源点到终点的最短路径 import java.util.ArrayList; import java. ...
- HDU1024 Max Sum Plus Plus(DP)
状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...
- hdu 1024 Max Sum Plus Plus(m段最大和)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- [剑指Offer]42-连续子数组的最大和(DP)
题目链接 https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&tqId=11183&t ...
- HDU1024 Max Sum Plus Plus —— DP + 滚动数组
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...
随机推荐
- Android偏好设置(1)概述和Preferences简介
1.Overview Instead of using View objects to build the user interface, settings are built using vario ...
- URAL1326. Bottle Taps(状压)
1326 用队列优化的 不知道为什么一直WA 传统直白的 状压 写了超时 O((1<<n)*m*n) 之后想了可以把n省去 预处理一下方案 #include <iostream&g ...
- 移动端rem单位用法
1.rem(font size of the root element)是指相对于根元素的字体大小的单位,em(font size of the element)是指相对于父元素的字体大小的单位.它们 ...
- C#_JDBC连接数据库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取
/** * 根据元数据和目标ascii位数截取字符串,失败返回-1 * @param sourceStr 元数据字符串 * @param endIndex 截取到第几位 * @return 结果字符串 ...
- 6 Specialzed layers 特殊层 第一部分 读书笔记
6 Specialzed layers 特殊层 第一部分 读书笔记 Specialization is a feature of every complex organization. 专注是 ...
- [Tunny]Grunt基础介绍
[黄映焜/Tunny,20140711] Grunt是一个JavaScript任务管理器,对于需要反复重复的任务,例如压缩.编译.单元测试.代码检查等,自动化工具可以减轻你的劳动,简化你的工作. 本文 ...
- 面向对象编程(OOP)基础知识(一)
Java是一个支持并发.基于类和面向对象的计算机编程语言. 下面列出了面向对象软件开发的优点: 1.代码开发模块化,更易维护和修改. 2.代码复用. 3.增强代码的可靠性和灵活性. 4.增加代码的可理 ...
- vue vueRouter vuex Axios webpack 前端常用内容
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.
- D. Gourmet choice并查集,拓扑结构
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...