点击打开链接

Max Sum

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

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

#include<iostream>
using namespace std;
int main()
{int N,n,a,first,last;cin>>N;for(int i=1;i<=N;i++){cin>>n;int max=-1000,sum=0,k=1;for(int j=1;j<=n;j++){cin>>a;sum=sum+a;if(sum>max)//如果当前的最大值大于以前的最大值  更新 sum 与max 都是一种计算的结果和属性相同{first=k;last=j;max=sum;}if(sum<0)//else{k=j+1;sum=0;}}cout<<"Case "<<i<<":"<<endl;cout<<max<<" "<<first<<" "<<last;if(i==N)cout<<endl;elsecout<<endl<<endl;}return 0;
}
#include <stdio.h>
int main()
{int z,n,max,sum;int a,b,A,B,t;scanf("%d",&z);for(int k=1;k<=z;k++){scanf("%d",&n);sum = max = -1001;for(int i=1;i<=n;i++){scanf("%d",&t);if(sum+t < t)sum = t , a = b = i;      //a、b记录当前连续子序列的起始、结束位置elsesum += t , ++b;if(max < sum)//附一篇另一种代码,max = sum , A = a , B = b;}printf("Case %d:\n%d %d %d\n",k,max,A,B);if(k-z) puts("");}return 0;
}

HDU_1003 Max Sum相关推荐

  1. HDU 1024:Max Sum Plus Plus(DP)

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

  2. 整数行hdu 1244 Max Sum Plus Plus Plus(dp)

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

  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

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

  5. J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题目大意:求最大的和,并输出初末位置 解题思路:两重for循环,第一重的意思是把 ...

  6. HD 1003 Max Sum (最大字段和问题)

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

  7. HD 1003 Max Sum(贪心)

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

  8. [云炬python学习笔记]Numpy中内置函数min(),max(),sum()与Python中内置函数min(),max(),sum()性能对比分析

    众所周知,Python有许多内置函数(例如min(),max(),sum()),Numpy也有自己的内置函数(np.min(),np.max(),np.sum()).由于Numpy的函数是在编译码中执 ...

  9. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

最新文章

  1. 应届生失业率或继续上升?别怕,这份秋招指南请收好!
  2. 神器:REST测试工具[wiztools.org restclient]客户端Jar依赖Java安装环境
  3. Android测试框架-uiautomator
  4. 【设计模式】中介者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )
  5. 用python计算2+4+6+…+20的值_计算2*3+(2*(5+6)*3)/2+4*6的值
  6. [LeetCode]: 100: Same Tree
  7. 【渝粤教育】国家开放大学2018年秋季 1317T社会工作行政(本) 参考试题
  8. larvel php restful_laravel 实现一个简单的 RESTful API
  9. linux 关闭scp服务器,Linux系统如何关闭scp和sftp命令
  10. checkbox:全选与反全选
  11. Tomcat使用Tomcat Native和APR
  12. 传智播客Java引用和数值类型思考
  13. 2022年电工杯B题应急物资配送问题浅析
  14. 控制系统--线性定常数系统的传递函数
  15. RGB色彩之间的过渡参考
  16. 计算机自我检测方法,电脑问题的自我检测方法有哪些?
  17. 2023年陕西师范大学宗教学考研上岸前辈备考经验指导
  18. 现场直播:域名转出的黑幕和愤怒!(商务中国BIZCN和美橙互联CNDNS)
  19. Opencv入门——读写图像、读写像素、修改像素值
  20. 面试前一些非技术注意事项--程序员面试金典

热门文章

  1. Fibonacci 数列
  2. Android获取手机短信
  3. Android 编译命令
  4. 如何将dtb反编译成dts
  5. Android JNI 使用的数据结构JNINativeMethod详解
  6. 基于区块链交易技术开发的证券
  7. 批量修改mp3文件的title等
  8. SOFA 源码分析 — 链路数据透传
  9. [Spring MVC] - @ModelAttribute使用
  10. Hive学习笔记 —— Hive概述