题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 158421    Accepted Submission(s): 37055

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 <cstdio>
 3
 4 using namespace std;
 5
 6 struct node
 7 {
 8     int s,e,smax;
 9 } sum[100010];
10
11 int main ()
12 {
13     int t,n,Max,k,j;
14     int num[100010];
15     while (~scanf("%d",&t))
16     {
17         int flag=1;
18         while (t--)
19         {
20
21             scanf("%d",&n);
22             for (int i=0; i<n; i++)
23             {
24                 scanf("%d",&num[i]);
25             }
26             sum[0].smax=num[0];
27             sum[0].s=sum[0].e=0;
28             k=0,j=0;
29             Max=sum[0].smax;
30             for (int i=1; i<n; i++)
31             {
32                 if (num[i]>sum[i-1].smax+num[i])
33                 {
34                     sum[i].smax=num[i];
35                     sum[i].s=i;
36                     sum[i].e=i;
37                 }
38                 else
39                 {
40                     sum[i].smax=sum[i-1].smax+num[i];
41                     sum[i].s=sum[i-1].s;
42                     sum[i].e=i;
43                 }
44                 if (Max<sum[i].smax)
45                 {
46                     Max=sum[i].smax;
47                     k=sum[i].s;
48                     j=sum[i].e;
49                 }
50             }
51             printf ("Case %d:\n",flag++);
52             printf ("%d %d %d\n",Max,k+1,j+1);
53             if (t)
54                 printf ("\n");
55         }
56     }
57     return 0;
58 }

转载于:https://www.cnblogs.com/qq-star/p/4269531.html

hdu 1003 Max Sum (DP)相关推荐

  1. HDU.1003 Max Sum

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

  2. hdu 1003 Max Sum 解题报告

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

  3. 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 ...

  4. [hdu 1003] Max Sum

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

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

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

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

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

  7. HDU 1244 Max Sum Plus Plus Plus

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

  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(dp)

    废话就不多说了,开始... Max Sum Plus Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

最新文章

  1. 【Networking】gRPC golang 相关资料
  2. robot连PCwifi、PC开Shell连robot
  3. vue java图片懒加载_vue 实现图片懒加载功能
  4. HDU 3584 三维树状数组
  5. 详解float**类型和float*类型
  6. 组合数学及其应用——鸽巢原理
  7. Linux安装gcc的四种方法
  8. 零基础CSS入门教程(26)–CSS按钮实例
  9. 第二章、音频压缩算法
  10. 想成为华为hcie网络工程师一定鸦知道的MSDP 基本介绍
  11. https开头的网址是什么意思_网址是什么意思?基础知识普及
  12. win2003系统的序列号
  13. Firefox 和 Firefox Nightly 同步服务:切换国内和国际服务器的方法
  14. YAMLException: bad indentation of a mapping entry at line 解决
  15. 我的STM32 IAP BOOT跳转到APP进入HardFault_Handler解决方案
  16. java毕业设计“西单”甜品线上预定系统mybatis+源码+调试部署+系统+数据库+lw
  17. 纸吸管,是下一个环保智商税吗?
  18. Unity如何完全消除摩擦力
  19. devops工具-Ansible基础
  20. 浅谈人机交互设计系统

热门文章

  1. SpringBoot使用netty
  2. 给RABBITMQ发送消息时,设置请求头HEADER
  3. Redis连接池Lettuce Jedis 区别
  4. Spring Boot之发送HTTP请求(RestTemplate详解)
  5. apache-maven仓库配置
  6. (转) Dockerfile 中的 COPY 与 ADD 命令 1
  7. js中函数参数值传递和引用传递
  8. [iOS]如何把App打包成ipa文件,然后App上架流程[利用Application Loader]
  9. cocos2dx[3.2](11)——新回调函数std::bind
  10. 【转】动态模型及其求解介绍–番外篇