杭电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 ...
随机推荐
- C++ STL标准模板库(list)
//list的使用 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<list> using namesp ...
- MFC自绘框架窗口客户区
利用MFC开发用户界面往往需要需要根据要求进行界面美化,界面的美化包括很多内容,比如说界面各功能模块空间布局,控件位置选择,各功能模块区域的字体.背景颜色选择.添加位图,标题栏.菜单栏.状态栏等的重绘 ...
- EJB EJB定义了一组可重用的组件:Enterprise Beans
EJB EJB定义了一组可重用的组件:Enterprise Beans.开发人员可以利用这些组件,像搭积木一样建立分布式应用.
- WPF MVVM(Caliburn.Micro) 数据验证
书接前文 前文中仅是WPF验证中的一种,我们暂且称之为View端的验证(因为其验证规是写在Xaml文件中的). 还有一种我们称之为Model端验证,Model通过继承IDataErrorInfo接口来 ...
- 在ChemDraw中一键隐藏所有氢原子的方法
在常见的化学结构中氢原子是非常常见的一种原子,而且在很多的结构中氢原子的数量是非常的多的.因此我们在使用ChemDraw化学绘图软件绘制化学结构的过程中,发现有的时候氢原子数量过多会影响到整体结构的美 ...
- Android 扁平化button
View 创建 colors.xml 文件定义两个颜色 1. <resources> 2. <color name="blue_pressed">@ ...
- Dapper的语法应用
(1)返回某个整型或字符串类型的字段 public string GetSupplierCodeById(int Id) { using( var conn=DbFactory.CreateConne ...
- Eclipse Spring Tool Suite插件安装
目录 Eclipse Spring Tool Suite插件安装 Eclipse Spring Tool Suite插件安装 1.登录网址:http://spring.io/tools/sts/all ...
- 巨蟒python全栈开发数据库攻略3:行记录的操作&单表查询3
1.数据行的增删改 2.单表查询 select&where条件 3.group by&having&order by&limit
- 巨蟒python全栈开发数据库前端3:CSS基础2
1.文本属性 2.背景属性 3.边框属性 4.display属性 5.盒子模型