K - Max Sum Plus Plus

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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(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

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n
Process to the end of file. 

Output

Output the maximal summation described above in one line. 

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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...

随机推荐

  1. swftools使用

    为了支持gif转swf以及pdf转swf.编译swftools过程中遇见几个问题,记录一下. 首先下载swftools:http://www.swftools.org/ 它依赖几个包,这里我使用的版本 ...

  2. Web项目中用mybatis配置多个数据库

    需要在项目中配置多个数据库(比如一个mysql,一个oracle)的时候,可按照如下方式配置 首先是第一个数据库的配置 <bean name="transactionManager&q ...

  3. mother&#39;s day.py 母亲节

    今天母亲节,写了个程序.抓取一个站点的母亲节祝福短信.实现自己主动翻页, 道友们也能够甲乙改造.比方加上节日简洁,time()模块. . . 一起分享吧 # -*- coding: cp936 -*- ...

  4. 利用gearman同步mysql数据到redis

    一.Gearman 1.Gearman是一个分发任务的程序框架. 2.体系:a.client:发送一个jobb.server:找到合适的worker,把job交给该workerc.worker:处理j ...

  5. jQuery 技术揭秘

    1.平时jquery调用页: index.html : <!DOCTYPE html> <html lang="en"> <head> < ...

  6. 已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”

    题目:已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”. 代码: <!DOCTYPE html> &l ...

  7. 谈谈PHP网站的防SQL注入

    SQL(Structured Query Language)即结构化查询语言.SQL 注入,就是把 SQL 命令插入到 Web 表单的输入域或页面请求参数的查询字符串中,在 Web表单向 Web 服务 ...

  8. Windows下创建.gitgnore文件

    相信使用过git的朋友可能遇到过,直接在windows下创建.gitgnore文件失败.类似截图那样 上网查了一下,有两种方法. 方法1: 此方法较为简单,前提是安装了git bash. 用git b ...

  9. MVC基础操作

    C#-MVC基础操作-数据的展示及增删改.登录页面及状态保持一.数据展示1.View代码: <%@Page Language="C#" Inherits="Syst ...

  10. linux 下mtime,ctime,atime分析

    一.atime.ctime与mtime atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的.网上很多资料都声称cat.more等读取文件的命令 ...