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- ...
随机推荐
- ES6里关于正则表达式的拓展
一.构造函数 在 ES5 中,RegExp构造函数的参数有两种情况. 第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag) var regex = new RegExp('xy ...
- c++11 std::prev、std::next、std::advance与auto 使用
auto 定义变量时放在变量前,无需知道具体变量类型,系统可自行推断类型,减少编程工作,特别是在模板使用时,使用更方便. 下面简单例子: auto a=; auto b='a'; auto s=&qu ...
- 转:Eclipse自动补全功能轻松设置
Eclipse自动补全功能轻松设置 || 不需要修改编辑任何文件 2012-03-08 21:29:02| 分类: Java | 标签:eclipse 自动补全 设置 |举报|字号 订阅 ...
- vue笔记三(组件)
十.组件 1.组件中的data为函数 2.props: 父组件向子组件传递数据 子组件:Child.vue <template> <span>{{ myMsg }}</s ...
- 常用的二种修改mysql最大连接数的方法
方法一:进入MYSQL安装目录 打开MYSQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MY ...
- 自定义 alert 弹窗
1.css样式 li-alert.css @-webkit-keyframes opacity { 0% { opacity: 0; /*初始状态 透明度为0*/ } 50% { opacity: 0 ...
- rabbitmq 用户和授权
官方文档 https://my.oschina.net/hncscwc/blog/262246?p=
- ios7中的edgesForExtendedLayout
edgesForExtendedLayout是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向. 因为iOS7鼓励全屏布局,所以它的默认值是UIRectEdgeAll——四周边缘都延 ...
- 了解.net mvc实现原理ActionResult/View
了解.net mvc实现原理ActionResult/View 上一篇了解了请求至Controller的Action过程,这篇继续看源码处理Action收到请求数据再返回ActionResult到Vi ...
- Paxos 学习总结
近期学习了分布式领域的重要算法Paxos,这里罗列下关键点当作总结.自己水平有限,难免存在谬误,恳请读者指正.本篇不包含Paxos的基本理论介绍.Paxos基础能够參考以下的学习资料章节. 1 Pax ...