HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35988 Accepted Submission(s): 12807
Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^
Process to the end of file.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}}; #define INF 0x7fffffff//无穷大
#define max_v 1000010
int a[max_v];
int now[max_v];// now[j]:包含第j个元素的最大和
int pre[max_v];// pre[j]:前j个元素的最大和,不包括第j个元素
int main()
{
int n,m,maxx;
while(~scanf("%d %d",&m,&n))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(now,,sizeof(now));
memset(pre,,sizeof(pre));
for(int i=;i<=m;i++)//m个子段
{
maxx=-INF;
for(int j=i;j<=n;j++)
{
now[j]=max(now[j-]+a[j],pre[j-]+a[j]);
//now[j] 有两种来源,一种是直接在第i个子段后面加a[j],一种是a[j]单独成为一个子段
pre[j-]=maxx;//更新pre使得pre[j-1]是前j-1个中的最大子段和
maxx=max(now[j],maxx);
}
}
printf("%d\n",maxx);
}
return ;
}
最新文章
- Spring MVC数据校验
- ABP源码分析二十六:核心框架中的一些其他功能
- SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块
- 初识Lucene.net
- Frag(匹配跟踪)
- 使用sql对数据库进行简单的增删改查
- [原创] zabbix学习之旅四:mail客户端安装
- 为cocos2d-x项目增加Lua支持
- P3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二
- Dedecms自定义sql 出现错误Safe Alert: Request Error step 2!
- Nginx源码研究一:NGINX模块启动
- 【高精度递推】【HDU1297】Children’s Queue
- 什么是JWT(JSON WEB TOKEN)
- 前段学习 之 webpack 学习记录
- Hive内部表与外部表的区别
- thinkphp5 tp5 与 nginx 搭配在根目录和子目录中如何设置伪静态
- 19_python_反射
- 运行结果出现Process finished with exit code 0
- Web API源码剖析之HttpServer
- Java编程模板