• 51nod 1049 最大子段和


    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注
    N个整数组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续子段和的最大值。当所给的整数均为负数时和为0。
    例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20。
    Input
    第1行:整数序列的长度N(2 <= N <= 50000)
    第2 - N + 1行:N个整数(-10^9 <= A[i] <= 10^9)
    Output
    输出最大子段和。
    Input示例
    6
    -2
    11
    -4
    13
    -5
    -2
    Output示例
    20

    尺取爬过

    emmmm

    #include <iostream>
    using namespace std;
    int a[50010];
    int main()
    {
    	long long n;
    	cin>>n;
    	for(int i = 0; i < n; i++)
    		scanf("%d",&a[i]);
    	long long max = a[0],sum = a[0];
    	for(int i = 1; i < n; i++)
    	{
    		if(a[i] + sum < a[i])
    			sum = a[i];
    		else
    			sum += a[i];
    		if(sum > max)
    		max = sum;
    	}
    	cout<<max<<endl;
    	return 0;
    }

  • 相关阅读:
    kibana ,logstash and filebeat
    The Run-Time Constant Pool The Constant Pool
    hsdb
    The Dataflow Model: A Practical Approach to Balancing
    编译器
    汇编
    状态机
    lsm-tree
    Serviceability
    JIT编译器
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270725.html
Copyright © 2020-2023  润新知