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" 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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤
1,000,000, -32768 ≤ S x ≤ 32767). We
define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ iy ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m)
instead. ^_^
Input
Each test case will begin with two integers m
Output
Output the maximal summation described above in one
line.
and n, followed by n integers S 1,
S2, S 3 ... S n.
Process to the end of file.
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
Hint
Huge input, scanf and dynamic programming is
recommended.
分析:d[j]为遍历到当前数字时,前j-1个数的i段区间和的最大值加上a[j],d[j]是动态的,可取可不取;
p[j]为记录前j个数的i段区间和的最大值;
j |
1 |
2 |
3 |
4 |
5 |
6 |
|
i |
-1 |
4 |
-2 |
3 |
-2 |
3 |
|
1 |
d |
-1 |
4 |
2 |
5 |
3 |
6 |
1 |
p |
-1 |
4 |
4 |
5 |
5 |
|
2 |
d |
-1 |
3 |
2 |
7 |
5 |
8 |
2 |
p |
-1e9 |
3 |
2 |
7 |
7 |
代码中p[n]没有记录,直接放在了ans中。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using
namespace std;
const int
Max=1e6+5;
int
p[Max],a[Max],d[Max];
int main()
{
int m,n,ans;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(p,0,sizeof(p));
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
ans=-1e9;
for(int j=i;j<=n;j++)
{
d[j]=max(d[j-1],p[j-1])+a[j];
p[j-1]=ans;
ans=max(ans,d[j]);
}
}
printf("%d\n",ans);
}
}
Max Sum Plus Plus HDU - 1024的更多相关文章
- 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想
/* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...
- C - Max Sum Plus Plus HDU - 1024
用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...
- 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)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 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 (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- (最大m子段和) Max Sum Plus Plus (Hdu 1024)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- Linux命令行抓包及包解析工具tshark(wireshark)使用实例解析
在Linux下,当我们需要抓取网络数据包分析时,通常是使用tcpdump抓取网络raw数据包存到一个文件,然后下载到本地使用wireshark界面网络分析工具进行网络包分析. 最近才发现,原来wire ...
- Maven的安装、配置及使用入门+maven安装报错:JAVA_HOME【申明:来源于网络】
Maven的安装.配置及使用入门+maven安装报错:JAVA_HOME[申明:来源于网络] Maven的安装.配置及使用入门:http://www.cnblogs.com/dcba1112/arch ...
- fiddler修改Requests之前的数据和response 之后的数据
1. 开启抓包 file--->capture traffic 2. 在页面底部黑框输入bpu http://www.runoob.com/?s=mysql 3. 在浏览器URL输入http:/ ...
- 【C++】约瑟夫环(数组+链表)
基于数组: #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- 通过Ajax方式上传文件(input file),使用FormData进行Ajax请求
<script type="text/jscript"> $(function () { $("#btn_uploadimg").click(fun ...
- flex总结一下
display:flex:规定元素是flex布局,里面的元素自然会像浮动一样横向排列: flex-direction:row | row-reverse | column | column-rever ...
- VisualStudioCode中用dotnet命令创建多个ASP.NET Core 项目、类库、控制台程序,并添加应用间的引用
一.准备工作 首先安装VisualStudioCode并且可以使用. 1.首先新创建空的MyApps文件夹,作为项目主目录,下面将在这个文件夹中创建多个web应用程序.类型.控制台程序等. 2.打开V ...
- JavaScript 区分中英文字符的两种方法: 正则和charCodeAt()方法
正则无疑是最强大的判断各种条件的方法, 最近也在研习它, 虽然枯燥, 但仍有乐趣. 用它来判断一个双字节的中文字符也是轻而易举地. 而判断中文字符, 简单且执行效率高. regExpForm.onb ...
- Google word/sheets 常见的使用:
Google Sheets: 1, sheets 里面的单元格设置自动换行: 选中单元格: --> Format --> Text Wrapping --> Wrap(自动换行)/C ...
- Oracle 10g RAC OCR、Voting disk更换
环境:OEL 5.7 + Oracle 10.2.0.5 RAC 需求:更换存储,OCR.Voting disk同时需要更换到新存储. 1.替换OCR 2.替换voting disk 1.替换OCR ...