Problem Description

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

Case 1:
14 1 4 Case 2:
7 1 6

Author

Ignatius.L


思路

最大连续子序列和问题,状态转移方程式:

\(f[i] = max(f[i-1]+a[i],a[i])\)

可以得出代码如下

代码

#include<bits/stdc++.h>
using namespace std;
const int INF = 1<<30;
int a[100001];
int main()
{
int n;
cin >> n;
for(int q=1;q<=n;q++)
{
int len;
cin >> len; int maxsum = -INF;
int currentsum = 0;
int l = 0,r = 0;
int tmp = 1;
for(int i=1;i<=len;i++)
{
cin >> a[i];
if(currentsum >= 0)
currentsum += a[i];
else
{
currentsum = a[i];
tmp = i;
}
if(currentsum > maxsum)
{
maxsum = currentsum;
l = tmp;
r = i;
}
}
cout << "Case " << q << ":\n";
cout << maxsum << " " << l << " " << r << endl;
if(q!=n) cout << endl;
}
return 0;
}

Hdoj 1003.Max Sum 题解的更多相关文章

  1. 最大子序列和 HDOJ 1003 Max Sum

    题目传送门 题意:求MCS(最大连续子序列和)及两个端点分析:第一种办法:dp[i] = max (dp[i-1] + a[i], a[i]) 可以不开数组,用一个sum表示前i个数字的MCS,其实是 ...

  2. HDOJ(1003) Max Sum

    写的第一个版本,使用穷举(暴力)的方法,时间复杂度是O(N^2),执行时间超过限制,代码如下: #include <stdio.h> #define MAX_LEN 100000UL in ...

  3. HDOJ 1003 Max Sum(线性dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 思路分析:该问题为最大连续子段和问题,使用动态规划求解: 1)最优子结构:假设数组为A[0, 1 ...

  4. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  5. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  6. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  7. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  8. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...

  9. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. Magic Stones CodeForces - 1110E (思维+差分)

    E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. CopyOnWriteArrayList源码分析

    基于jdk1.7源码 一.无锁容器 CopyOnWriteArrayList是JDK5中添加的新的容器,除此之外,还有CopyOnWriteArraySet.ConcurrentHahshMap和Co ...

  3. 学习WebSocket

    初识WebSocket: index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  4. 【转】linux下查看磁盘分区的文件系统格式

    https://www.cnblogs.com/youbiyoufang/p/7607174.html

  5. .net 报错汇总——持续更新

    1.未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPla PM> Install-Package Microsoft ...

  6. Laravel 核心--Facades 门面

    Laravel 核心--Facades 门面 伊Summer 关注  0.1 2017.08.12 19:07* 字数 2017 阅读 1089评论 0喜欢 5 介绍 Facades 为应用的 IoC ...

  7. 老男孩python学习自修第四天【字典的使用】

    dict = {key1:value1, key2:value2} 定义字典 dict[key] = value 设置字典中指定健的值 dict.pop(key) 删除字典中指定健 dict.popi ...

  8. npm 设置代理

    设置代理 npm config set proxy http://username:password@server:portnpm config set https-proxy http://user ...

  9. vue-cli(vue脚手架)

    vue-cli用于自动生成vue+webpack项目. 安装webpack:npm install webpack -g 检查webpack是否安装成功和版本:webpack -v 如果是webpac ...

  10. python之读取和写入csv文件

    写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...