question:

Max Sum

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

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
 
idea:
add up the sum each input
memory the max,and update it when the sum more than the max
set the sum as zero when the sum is less than zero,and meanwhile,set the temp as current order of the number and add two
Thinking process:if the sum is still more than zero ,next addition must bigger than it,and it must be less then the last when the sum is negative number,so initialize the sum only when it is negative
 
source code :
 
import java.util.Scanner;

public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// int[] arr = {1,1,3,-2,-4,5,6};
// System.out.println(rec_opt(arr,6));
// System.out.println(rec_opt_circle(arr));
// System.out.println(new int[2][3].length);
// System.out.println(sum_extract(arr,arr.length-1,9));
int group_amount = sc.nextInt();
int counter = 1;
while((group_amount--)>0){
int start = 0;
int max = -9999;
int end = 0;
int sum = 0;
int temp = 1; int len = sc.nextInt(); for (int i = 0;i<len;i++){
sum+= sc.nextInt();
if(sum > max){
max = sum;
start = temp;
end = i+1;
} if(sum<0){
sum = 0;//重新初始化,然后跳转到当前位置重新开始累加
temp = i + 2;
}
}
/**
* Case 1:
* 14 1 4
*/
System.out.println("Case "+(counter++)+":");
System.out.println(max + " " + start + " " + end);
if(group_amount!=0)System.out.println();
}
}
}
hope that I can help you guys XD
that's all
 
 
 

算法_hdoj_1003的更多相关文章

  1. B树——算法导论(25)

    B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...

  2. 分布式系列文章——Paxos算法原理与推导

    Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...

  3. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  4. 红黑树——算法导论(15)

    1. 什么是红黑树 (1) 简介     上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...

  5. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  6. 虚拟dom与diff算法 分析

    好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM

  7. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  8. 神经网络、logistic回归等分类算法简单实现

    最近在github上看到一个很有趣的项目,通过文本训练可以让计算机写出特定风格的文章,有人就专门写了一个小项目生成汪峰风格的歌词.看完后有一些自己的小想法,也想做一个玩儿一玩儿.用到的原理是深度学习里 ...

  9. 46张PPT讲述JVM体系结构、GC算法和调优

    本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...

随机推荐

  1. Java中的isEmpty方法、null以及""的区别

    本文转自:https://blog.csdn.net/peng86788/article/details/80885814 这是一个比较容易混淆的概念,为了弄清楚这个问题,最好的方法当然是写程序来验证 ...

  2. css中content-box和border-box当宽度为百分比时的位置区别,vw和%区别

    盒模型 参考代码 // CSS 部分 <style> .box1,.box2{ width: 200px; height: 200px; padding: 20px; margin: 20 ...

  3. bugkuCTF-管理员系统(IP伪造)

    题目地址:http://123.206.31.85:1003/ 登进去是一个管理员后台登录的样子 试了sql的万能密码,发现进不了,而且下面还报错了ip禁止 禁止了我们的ip,但是他本地的ip肯定没有 ...

  4. 使用hexo+github搭建免费个人博客详细教程(转载)

    https://www.cnblogs.com/liuxianan/p/build-blog-website-by-hexo-github.html 1.上传文档的hexo常用命令 2.输入hexo ...

  5. [CF1303B] National Project - 数学

    Solution \(2a>n\),一次性结束,直接输出 \(n\) \(a \geq b\),那么一直修即可,直接输出 \(n\) 否则,\(a\) 占弱势,我们考虑用 \(a\) 修一半需要 ...

  6. php安装xdebug扩展,PHPStorm+XDebug单步调试

    (一)php安装xdebug扩展,PHPStorm+XDebug单步调试 (二)PHPStorm配置XDebug (三)PHPStorm使用XDebug调试 (四)PhpStorm+Xdebug配置单 ...

  7. string类型的应用场景 —— Redis实战经验

    string类型是实战中应用最多的数据类型,Redis的一些特性决定了string类型的应用场景. 1. Redis的数据是共享的 如果将用户信息存储在web服务的本地缓存,则每个web服务都会缓存一 ...

  8. 爬虫爱用的一些python技巧

    1.正则表达式 有时候提取到的数据不规整,需要用正则来匹配所需要展现出来的数据 学习链接:https://www.cnblogs.com/-chenxs/p/11352172.html,https:/ ...

  9. SKlearn | 学习总结

    1 简介 scikit-learn,又写作sklearn,是一个开源的基于python语言的机器学习工具包.它通过NumPy, SciPy和Matplotlib等python数值计算的库实现高效的算法 ...

  10. 牛客网——剑指offer(跳台阶以及变态跳台阶_java实现)

    首先说一个剪枝的概念: 剪枝出现在递归和类递归程序里,因为递归操作用图来表示就是一棵树,树有很多分叉,如果不作处理,就有很多重复分叉,会降低效率,如果能把这些分叉先行记录下来,就可以大大提升效率——这 ...