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. [P5665][CSP2019D2T2] 划分

    先说说部分分做法吧 1.\(n \leq 10\) 指数级瞎草都可以2333 2.\(n \leq 50\) 好像并没有什么做法-也许给剪枝的人部分分吧 3.\(n \leq 400\) 这个复杂度是 ...

  2. CF718C Sasha and Array [线段树+矩阵]

    我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...

  3. 布隆过滤器--guava实现

    private static int size = 1000000;//预计要插入多少数据 private static double fpp = 0.01;//期望的误判率 private stat ...

  4. 检测到 LoaderLock Message: 正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。

    解决方法: 调试状态=>异常(Ctrl+Alt+E)=>Managed Debuggin Assistants=>LoaderLock 的选中状态去掉即可.

  5. js监听textarea 内容的变化,并计算内容的长度c

    html代码如下:   <div class="customer-message">   <label for="customerMessage&quo ...

  6. java - CAS及CAS底层原理

    CAS是什么? CAS的全称为Compare-And-Swap它是一条CPU并发原语,也就是在CPU硬件层面上来说比较并且判断是否设置新值这段操作是原子性的,不会被其他线程所打断.在JAVA并发包ja ...

  7. Luogu1287 | 盒子与球 (排列组合)

    贴一个和其他题解不一样的做法 QWQ 题意:让我们求出 N 个球放入 R 个盒子且每个盒子都必须放球方案数. 首先,对于每一个球,可以将其放入的盒子数量共有 R 个,所以我们可以知道如果无需满足每个盒 ...

  8. Flask知识总结

    1.-----------------路由设置的2种方式----------------- 查看源码,route方法里,本质是执行app.add_url_rule() 因此可以这么写(主流方式): @ ...

  9. 使用python 写一个 股票涨跌提醒功能

    1: 安装python: https://www.cnblogs.com/liyafei/articles/11550979.html 将python.exe加入 全局path 2: 安装库:(不需要 ...

  10. [BOI2008] Elect - 背包dp

    u1s1我一开始理解错了题 然后基本就相当于一个背包dp了 #include <bits/stdc++.h> using namespace std; int n,tot,a[305],f ...