动态规划 hdu 1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41033 Accepted Submission(s):
14763
Sum" problem. To be a brave ACMer, we always challenge ourselves to more
difficult problems. Now you are faced with a more difficult
problem.
Given a consecutive number sequence S1,
S2, S3, S4 ...
Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤
Sx ≤ 32767). We define a function sum(i, j) =
Si + ... + Sj (1 ≤ i ≤ j ≤ n).
Now
given an integer m (m > 0), your task is to find m pairs of i and j which
make sum(i1, j1) + sum(i2,
j2) + sum(i3, j3) + ... +
sum(im, jm) maximal (ix ≤
iy ≤ jx or ix ≤
jy ≤ jx is not allowed).
But I`m
lazy, I don't want to write a special-judge module, so you don't have to output
m pairs of i and j, just output the maximal summation of
sum(ix, jx)(1 ≤ x ≤ m) instead.
^_^
followed by n integers S1, S2,
S3 ... Sn.
Process to the end of
file.
line.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended;
int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m;
arr[ ]储存输入的数;
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
用temp来找出 j 个数取 k 组所得的组的最大和;同时把它记入在pre[ ]中,用于进行下次更新;
dp[j]=max(dp[j-1],pre[j-1])+arr[j];此时dp[j]和dp[j-1]有同样的组数,
dp[j]=dp[j-1]+arr[j]; // 表示把第j个数加入dp[j-1]的其中一组,能保持组数不变;
dp[j]=pre[j-1]+arr[j]; // 表示让第j个数独成一组,再加上比dp[j]少一组的组集中的最大的;
两者中选择大的组合方式;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
const int inf=0x3f3f3f3f; int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m; int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&arr[i]);
}
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return ;
}
动态规划 hdu 1024的更多相关文章
- 动态规划-----hdu 1024 (区间连续和)
给定一个长度为n的区间:求m段连续子区间的和 最大值(其中m段子区间互不相交) 思路: dp[i][j]: 前j个元素i个连续区间最大值 (重要 a[j]必须在最后一个区间内) 转移方程:dp[i][ ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 怒刷DP之 HDU 1024
Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- Max Sum Plus Plus HDU - 1024
Max Sum Plus Plus HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 Max Sum Plus Plus (动态规划、最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1024(滚动数组+动态规划)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...
- [Linux]ln -al输出内容解析
第一项的第一位是文件类型:-: 表示普通文件d: 目录b: 块特殊文件c: 字符特殊文件l: 符号链接文件p: 命名管道文件FIFOs: 套接字文件 第二项是该文件的inode的链接计数,即stat ...
- Gathering Fingerprinting
1. Banner grabbing with Netcat Netcat is multipurpose networking tool that can be used to perform mu ...
- Win10安装java环境
window系统安装java 一.下载JDK 1.首先需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downl ...
- windows下创建MySQL定时备份与删除脚本
今天在windows服务器上面写了一个MySQL定时任务,备份呢与删除 rem *****************************Code start********************* ...
- UOJ#185. 【ZJOI2016】小星星 容斥原理 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ185.html 题解 首先暴力DP是 $O(3^nn^3)$ 的,大家都会. 我们换个方向考虑. 假设我们 ...
- .net 本地日志的添加
/// <summary> /// 写入日志到文本文件 /// </summary> /// <param name="userName">用户 ...
- Doctrine2-完整创建数据库
---恢复内容开始--- 下面用Doctrine2设置配置一个数据库,具体分为以下步骤 1.设计主要字段和表 2.配置symfony2和数据库的连接 3.配置Entity,定义类 4.实现get/se ...