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. ios开发 Socket通信

    Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通信的双发,即客户端(Clien ...

  2. hive bucket

    转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html hive中table可以拆分成partition,table和partit ...

  3. 迁移Veil:手工打造Windows下编译的免杀Payload

    作者:RedFree 本文转自乌云 Veil对Payload的免杀已经做的很好了,最新的Veil有39个可用的Payload.但是有时候需要使用Windows来完成所有的渗透测试工作,Linux和Wi ...

  4. mysql中show processlist过滤和杀死线程

    select * from information_schema.processlist where HOST LIKE '%192.168.1.8%'; kill ID列

  5. 使用TP自带缓存时。出现第一次拿不到数据。

    使用TP自带缓存时.出现第一次拿不到数据. 仔细检查逻辑发现了问题所在. 逻辑:直接读缓存,如果没有从数据库查询,然后存入缓存. 问题出在以为$exchange = S($fileName,$exch ...

  6. 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记30 ScrollView Demo实战

    在上一话中我们创建了一个通过URL读取图片的Demo,这个Demo是不能拖动和缩放的.如今给它添加选项让它能够手动切换URL,并把图片加入到ScrollView中. 向Storyboard中拖入一个s ...

  7. C++ 智能指针详解(转)

    C++ 智能指针详解   一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常 ...

  8. TortoiseGit 连接oschina不用每次输入username和password的方法

    每次git clone 和push 都要输入username和password.尽管安全.但在本机上每次都输有些麻烦,怎样记住username和password呢? 在网上看了各种方法,太杂,非常多可 ...

  9. shader之旅-7-平面阴影(planar shadow)

    根据<real-time shadow>这本书第二章中的推导,实现了最简单的阴影技术. planar shadow通过一个投影矩阵将被灯光照射的物体的顶点沿着光线方向投影到接受阴影的平面. ...

  10. linux LVS (keepalived+ipvsadm)负载均衡搭建

    ipsvadm 配置 一:安装配置 LVS+DR+Keepalivedclient 访问的地址 VIP 192.168.133.100       负载服务器master真实IP        192 ...