题目回顾(HDU-1003):

Max Sum

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
源码与解析
 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int main(){
 5     int T,N;
 6     int a[100010];
 7     cin>>T;
 8     for(int k=1;k<=T;k++){
 9         cin>>N;
10         for(int i=0;i<N;i++){
11             cin>>a[i];
12         }
13         int sum=0;
14         int ans=-10000;            //如果设置的数字不够小,出错。例如sum=-500,
15                                   //初始ans=-10,sum<ans无法标记下一步位置。
16         int start,end;             //标记起点和终点。
17         int temp=0;                //temp要初始化起点位置。
18         for(int i=0;i<N;i++){
19             if(sum>=0){
20                 sum+=a[i];
21             }else{
22                 sum=a[i];
23                 temp=i;            //暂时保存新起点位置。
24             }
25
26             if(sum>ans){           //如果发现更大的sum,则保存起点和终点。
27                 ans=sum;
28                 start=temp;
29                 end=i;
30             }
31         }
32         cout<<"Case "<<k<<":"<<endl<<ans<<" "<<start+1<<" "<<end+1<<endl;
33         if(k<T){                  //输出格式的要求。
34             cout<<endl;
35         }
36     }
37     return 0;
38 } 

转载于:https://www.cnblogs.com/orangecyh/p/9762270.html

HDU-1003 Max Sum(动态规划)相关推荐

  1. HDU.1003 Max Sum

    原题 HDU.1003 Max Sum 分类 动态规划 题意 计算从一个序列中最大连续子序列和.对应的起始元素和终止元素的位置. 输入/输出 要求与格式 样例数的确定 最开始一行开始输入样例数 每个样 ...

  2. hdu 1003 Max Sum (DP)

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

  3. hdu 1003 Max Sum 解题报告

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem Description Given a sequence a[1],a[2],a[3 ...

  4. [hdu 1003] Max Sum

    跟<算法导论(第三版)>上的一样,抄下来的 30 - 60 ms 标准的分治策略 1 #include <stdio.h> 2 3 int A[100000], Lenght; ...

  5. hdu 1003 Max Sum

    DP可以这里给出非DP程序 题目传送门 1 #include<stdio.h> 2 int main() 3 { 4 int t,n,i,max,m,sum,ki,kj,k,a,b; 5 ...

  6. HDU 1003——Max Sum(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:历遍所有数字,找出最大字段和. 解题思路: t和n:记录循环次数和每一段有多少个数字 ...

  7. 位置子段最大子段和 hdu 1003 max sum ACM的开始

    每日一贴,今天的内容关键字为位置子段 这个标题的印象是较比刻深的,我的第一个动态规划标题,也是ACM的开始.最大字段和是较比经典的动归问题.求一个列序的最大子段和的关键点就在于断判一个元素的归属问题, ...

  8. 杭电OJ——ACM 1003.Max Sum

    Max Sum 杭电OJ--ACM 1003.Max Sum链接入口 问题描述 大意:        给定一个序列a[1],a[2],a[3]-a[n],你需要算出其子序列中的最大值.比如说:给你一个 ...

  9. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  10. HDU 1024 Max Sum Plus Plus 动态规划

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目大意:n个数分成两两不相交的m段,求使这m段和的最大值. 解题思路:比较坑的点:n2 能过: ...

最新文章

  1. Redis源码解析——前言
  2. Java Date Time 教程-java.sql.Timestamp
  3. [React Native] 解析JSON文件
  4. LeetCode 18 四数之和
  5. Xftp5解决“要继续使用此程序,您必须应用最新的更新或使用新版本”
  6. 《T-SQL性能调优秘笈——基于SQL Server 2012 窗口函数》——1.2 使用窗口函数的解决方案简介...
  7. 【竞赛题解】Codeforces Round #710 (Div. 3)
  8. 谷歌、脸书、魔兽世界都在用!InnoDB是什么?有哪些关键特性?
  9. 执行sql语句_SQL查询语句的执行顺序解析
  10. 苹果开机启动参数说明
  11. 公钥、私钥、数字签名和数字证书的概念及解密
  12. 知到智慧树答案2020python_2020知到智慧树大数据分析的python基础章节答案
  13. 深入浅出 MFC_华中理 工_简体版电子书pdf下载
  14. 分享几个软件测试逻辑思维和综合面试题【含答案】
  15. linux 清理垃圾指令,Linux清理系统垃圾
  16. 2021西湖论剑网络安全大赛部分WP
  17. JVM之常用的字节码指令(基于《深入理解Java虚拟机》之第六章类文件结构)(下)
  18. 給windowsXP穿上Linux Ubuntu的漂亮馬甲 1
  19. 初识js-charts和E-charts
  20. Shiro 第十七章 OAuth2集成

热门文章

  1. ARM不同位数系统int字节数区别
  2. Cortex M3内核架构
  3. python网络爬虫系列(九)——打码平台的使用
  4. Packet Tracer 5.0 建构 CCNA 实验攻略——路由器实现 Vlan 间通信
  5. LeetCode 1887. 使数组元素相等的减少操作次数(map)
  6. LeetCode 1665. 完成所有任务的最少初始能量(贪心)
  7. LeetCode 875. 爱吃香蕉的珂珂(二分查找)
  8. 程序员面试金典 - 面试题 05.08. 绘制直线(位运算)
  9. 二叉排序树的后序遍历序列必然是递增的_剑指offer 33——二叉搜索树的后序遍历序列...
  10. 【机器学习-数据科学】第二节:ipython开发环境搭建以及pandas快速入门