杭电1024Max Sum Plus Plus
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024
题目:
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.
| dp[i]\a[i] | 2 | -1 | 3 | -8 | 9 |
| 1 | 2 | ||||
| 2 | 1 | ||||
| 3 | 4 | ||||
| 4 | -4 | ||||
| 5 | 9 |
| dp i\j) | -1 | 4 | -2 | 3 | -2 | 3 |
| 1 | -1 | 4 | 2 | 5 | 3 | 6 |
| 2 | \ | 3 | 2 | 7 | 5 | 8 |
| 3 | \ | \ | 1 | 6 | 5 | 10 |
| 4 | \ | \ | \ | 4 | 4 | 9 |
| 5 | \ | \ | \ | \ | 2 | 7 |
| maxtemp | \ | \ | \ | \ | \ | \ |
| 1 | -1 | 4 | 4 | 5 | 5 | \ |
| 2 | \ | 3 | 3 | 7 | 7 | \ |
| 3 | \ | \ | 1 | 6 | 6 | \ |
| 4 | \ | \ | \ | 4 | 9 | \ |
| 5 | \ | \ | \ | \ | 2 | \ |
| \ | \ | \ | \ | \ |
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define N 1000010
#define PI acos((double)-1)
#define E exp(double(1))
using namespace std;
int dp1[N];
int maxtemp[N];
int a[N]; int main(void)
{
int n, m, max1;
while (scanf("%d%d", &m, &n) == )
{
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
dp1[] = maxtemp[] = max1 = a[];
for (int i = ; i <= n; i++)
{
if (max1 >= )
{
dp1[i] = a[i] + max1;
max1 += a[i];
}
else
{
dp1[i] = a[i];
max1 = a[i];
}
}
for (int i = ; i <= m; i++)
{
maxtemp[i-] = dp1[i-];
for (int j = i ; j < n; j++)
{
if (maxtemp[j - ]<dp1[j])
{
maxtemp[j] = dp1[j];
}
else
maxtemp[j] = maxtemp[j - ];
}
for (int j = i; j <= n; j++)
{
if (i == j)
dp1[j] = maxtemp[j - ] + a[j];
else
dp1[j] = max(dp1[j - ], maxtemp[j - ]) + a[j];
}
}
max1 = dp1[m];
for (int i = m; i <= n; i++)
if (max1<dp1[i])
max1 = dp1[i];
cout << max1 << endl; }
return ;
}
杭电1024Max Sum Plus Plus的更多相关文章
- 杭电1024----Max Sum Plus Plus
/* 这题还没有理解透彻.某个dalao也不写注释.只能自己理解了... 先求为i个元素(1<=i<=M)为一个区间的最大和,保证元素个数大于等于i个,递推到M个即可 借鉴原址:http: ...
- 杭电1003-Max Sum
Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...
- 杭电1003_Max Sum
这是原题的链接http://acm.hdu.edu.cn/showproblem.php?pid=1003 起初我是利用暴力的方法,求出所有序列的和的情况,每取一个序列就和以知道的最大和作对比,取大者 ...
- 杭电ACM2058--The sum problem
http://acm.hdu.edu.cn/showproblem.php?pid=2058 以为简单的穷举就完了,结果是一直Time Limit Exceeded.. 这是代码: #include ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- Help Johnny-(类似杭电acm3568题)
Help Johnny(类似杭电3568题) Description Poor Johnny is so busy this term. His tutor threw lots of hard pr ...
随机推荐
- dd & cpio
dd: ------------------------------------------------------ - 指定大小块的拷贝一个文件 例1. 想把软盘的内容拷贝到另一个软盘 dd i ...
- [Idea Fragments] PostScript for 3D Print??
今天看到一篇关于PostScript的文章<编程珠玑番外篇-P PostScript 语言里的珠玑>,尤其是篇尾的这段话,让我对3D Print浮想联翩: 因为 PostScript 语言 ...
- 第一百四十九节,封装库--JavaScript,表单验证--验证用户名
封装库--JavaScript,表单验证--验证用户名 注册验证功能,顾名思义就是验证表单中每个字段的合法性,如果全部合法才可以提交表单. 效果图 聚集光标时 信息不合法是 信息合法时 html &l ...
- python urllib2导出elasticsearch数据时 返回 "urllib2.HTTPError: HTTP Error 500: Internal Server Error"
0.业务场景 将ES中某个index的某个字段的所有数据,导出到文件中 1.ES数据导出方法简述 ES数据导出方法,我主要找到了以下几个方面,欢迎大家补充: ES官方API:snapshot and ...
- Android基站定位
Android基站定位 一.通过手机信号获取基站信息 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释: MCC.Mobile Country C ...
- SPAF模板
#include <iostream> #include <cstring> #include <queue> #include <cstdio> #d ...
- SQL Server 数据库分离与附加(图文教程)
from:http://www.jb51.net/article/36624.htm 一.概述 SQL Server提供了“分离/附加”数据库.“备份/还原”数据库.复制数据库等多种数据库的备份和恢复 ...
- 关于在ubuntu平台下使用apt-get命令下载速度太慢的问题解决
1. 进入设置,从哪进都一样找到就行. 2.选择软件与更新(Software and updates,英语估计是这个把) 点击下载自:这个下拉框,选中其它站点,选择镜像 选择阿里的或者搜狐的镜像,然后 ...
- [转]C#自定义控件属性与行为
原文链接:http://blog.csdn.net/a237428367/article/details/5926445 控件应该定义属性而不是公共字段,因为可视化设计器在属性浏览器中显示属性,而不显 ...
- Java每日一题
1.(单选题)What will be printed when you execute the following code? class C { C() { System.out.print(&q ...