K - Max Sum Plus Plus
K - Max Sum Plus Plus
Description
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(im, j m) maximal (i x ≤ i y ≤ 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
Process to the end of file.
Output
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.
//题意是:第一行 m ,n (n<=1000000) 两个整数,然后第二行 n 个数,求 m 段不相交连续序列最大和。
这篇博客写得十分详细
http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html
dp[i][j]代表的状态是 i 段连续序列的最大和,并且最后一段一定包含 num[j]
所以写出状态转移方程 dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} 0<t<j-1
dp[i][j-1]代表接上上一个元素,后面代表自己独立成一组
n很大,只能用滚动数组
不断更新状态,用一个数据 big 保存 i - 1 段最大的和,最后直接输出,就是答案
436ms
#include <stdio.h>
#include <string.h> #define inf 0x7ffffff
#define MAXN 1000005
int num[MAXN];
int dp[MAXN];
int pre[MAXN]; int max(int a,int b)
{
return a>b? a:b;
} int main()
{
int m,n;
int i,j,big;
while (scanf("%d%d",&m,&n)!=EOF)
{
for (i=;i<=n;i++)
{
scanf("%d",&num[i]);
pre[i]=;
} pre[]=;
dp[]=; for (i=;i<=m;i++)
{
big=-inf;
for (j=i;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+num[j];//连续的最大和,或者不连续的最大和
pre[j-]=big; //保存 i - 1 段 最大和
big=max(big,dp[j]);//保证是 i 段最大的和
}
}
printf("%d\n",big);
}
return ;
}
K - Max Sum Plus Plus的更多相关文章
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- Leetcode: Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 【leetcode】363. Max Sum of Rectangle No Larger Than K
题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...
- Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [Swift]LeetCode363. 矩形区域不超过 K 的最大数值和 | Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 363. Max Sum of Rectangle No Larger Than K
/* * 363. Max Sum of Rectangle No Larger Than K * 2016-7-15 by Mingyang */ public int maxSumSubmatri ...
- 363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] 363. Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
随机推荐
- 自助采样法 bootstrap 与 0.632
- django模型manager学习记录
Managers 在语句Book.objects.all()中,objects是一个特殊的属性,需要通过它查询数据库. 在第5章,我们只是简要地说这是模块的manager .现在是时候深入了解mana ...
- 前端打包利器:webpack工具
一.什么是WebPack,为什么要使用它? 1.为什要使用WebPack 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化开发的复杂度,前端 ...
- 【Hive】Hive 基础
Hive架构: Hive基础 1 概念 1.1 简介 1.1.1 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表, 并提供简单的sql查询功能,可以将sql语句 ...
- EF使用延迟加载的本质原因
EF(Entity Framework)是微软的一个ORM框架 使用过EF的同学都知道它有一个延迟加载的功能 那么这个延迟加载的功能到底是什么? 为什么需要延迟加载? 使用延迟加载的优点和缺点又各是什 ...
- angular 中的$event 对象包含了浏览器原生的event对象
ou can pass the $event object as an argument when calling the function. The $event object contains t ...
- IOS 开发环境,证书和授权文件是什么?
一.成员介绍 1. Certification(证书) 证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种: 1) Developer Certification(开发证书) 安装 ...
- FolderBrowserDialog 关于设置为单线程单元(STA)模式的问题
当Main函数是这样的状态的时候,当打开FolderBrowserDialog控件的时候 ,报错 这里有两种解决办法,第一种,就是把main 上加[STAThread] 第二种是启用一个线程 Thre ...
- wps文档忘记保存关闭了怎么恢复
wps文档忘记保存关闭了怎么恢复 点击程序左上角的''WPS文字/表格/演示''选择备份管理,根据需要尝试右侧下面的"查看其他备份"功能就能找了. 点击"开始-运行&qu ...
- B树、B-树、B+树、B*树(转)
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...