杭电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 ...
随机推荐
- pycharm重置设置,恢复默认设置
备忘,备忘,备忘 window 系统 找到下方目录-->删除. 再重新打开pycharm # Windows Vista, 7, 8, 10: <SYSTEM DRIVE>\User ...
- 05 Java图形化界面设计——布局管理器之GridLayout(网格布局)
网格布局特点: l 使容器中的各组件呈M行×N列的网格状分布. l 网格每列宽度相同,等于容器的宽度除以网格的列数. l 网格每行高度相同,等于容器的高度除以网格的行数. l 各组件的排列方式 ...
- HDU-1095-A+B for Input-Output Practice (VII)(多一个空格?)
A+B for Input-Output Practice (VII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- Dependency Property 依赖属性
依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值(依赖在别人身上)的属性.拥有依赖属性的对象称为“依赖对象”. WPF开发中,必须使用依赖对象作为依赖属性的宿主,使二者结合起来 ...
- ChemDraw绘制DNA结构的技巧
对生物有一定了解的朋友都知道DNA是染色体的重要组成部分,DNA结构中包含重要的遗传物质,孩子的DNA来自父母DNA的组合,这就是为什么“一家人相像”的奥秘所在.ChemDraw虽然号称是化学结构绘制 ...
- poj 3653(最短路)
题目链接:http://poj.org/problem?id=3653 思路:题目意思很简单,就是二维平面上的图,要求起点到终点的最短路.建图略坑,需要坐标映射,化二维为一维.然后就是Dijkstra ...
- C#实现动态编译代码
/*------------------------------------------------------------------------------ * Copyright (C) 201 ...
- sql查询用nolock
大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 什么是并发访问:同一时间有多个用户访问同一资源,并发 ...
- poj2046
Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1829 Accepted: 829 Description Le ...
- shop++源码反编译----随笔
一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...