URAL 1296. Hyperjump(最大子序列和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1296

Developed in the beginning of XXI century, hyperjump remains the primary method of transportation for distances up to thousands parsecs. But physicists have recently discovered an amazing phenomenon.
They believe the duration of the hyperjump alpha phase can be easily controlled. Alpha phase is the period when hyper-spacecraft accumulates its gravity potential. The larger is the gravity potential accumulated, the less energy is required to complete the
hyperjump. Your task is to write a program, which would help pilots decide when to enter and when to leave the alpha-phase, in order for the hyperspacecraft to accumulate the largest possible gravity potential.

The most crude gravity field model (which you will have to use) yields the sequence of integers pi, which represent
field intensities at different moments in time. According to this model, if the alpha-phase begins at moment i and ends at moment j, then the value of gravity potential accumulated will be equal to the sum of sequence elements at places from i-th
to j-th inclusive.

Input

The first line of the input contains an integer N being the number of elements in the intensity values sequence (0 ≤ N ≤ 60000). Next N lines specify sequence
elements, each line containing a single integer pi (?30000 ≤ pi ≤ 30000).

Output

The only line of output contains the largest possible value of the gravity potential that can be accumulated by a hyperspacecraft during the alpha phase. You should assume that the initial gravity potential
of a hyperspacecraft is equal to zero.

Samples

input output
10
31
-41
59
26
-53
58
97
-93
-23
84
187
3
-1
-5
-6
0

PS:

如果最小和小于零 ,输出零!

代码如下:

#include <cstdio>
int main()
{
    int n;
    int a[60047];
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        int maxx = 0, sum = 0;
        for(int i = 0; i < n; i++)
        {
            sum+=a[i];
            if(sum <= 0)
            {
                sum = 0;
            }
            if(sum > maxx)
            {
                maxx = sum;
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}
时间: 2024-10-08 23:38:23

URAL 1296. Hyperjump(最大子序列和)的相关文章

POJ 2774 Long Long Message &amp;&amp; URAL 1517. Freedom of Choice(求最长重复子序列)

两个题目意思差不多,都是让求最长公共子串,只不过poj那个让输出长度,而URAL那个让输出一个任意的最长的子串. 解体思路: Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 22313   Accepted: 9145 Case Time Limit: 1000MS Description The little cat is majoring in physics in the cap

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

Ural 1146 Maximum Sum(DP)

题目地址:Ural 1146 这题是求最大子矩阵和.方法是将二维转化一维. 首先用n*n的方法来确定矩阵的列.需要先进行预处理,只对每行来说,转化成一维的前缀和,这样对列的确定只需要前后两个指针来确定,只需要用前缀和相减即可得到.前后两个指针用n*n的枚举. 确定好了哪几列,那么再确定行的时候就转化成了一维的最大连续子序列的和.再来一次O(n)的枚举就可以. 这样,总复杂就变成了O(n^3),对于n为100来说,已经足够了. 代码如下: #include <iostream> #include

最大子矩阵和 URAL 1146 Maximum Sum

题目传送门 1 /* 2 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 3 好像之前做过,没印象了,看来做过的题目要经常看看:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 using namespace std; 10 11 const int MAXN = 1e2 + 10; 12 co

14-高效求最长公共子序列(二维数组存不下)

/*                                   See LCS again时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述 There are A, B two sequences, the number of elements in the sequence is n.m; Each element in the sequence are different and less than 100000. Calculate the length

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

最大连续子序列和

对于给定的数组 numnum,一个长度为 ss 的连续子序列是指由 num_i,num_{i+1},num_{i+2}\ldots,num_{i+s-1}num?i??,num?i+1??,num?i+2??…,num?i+s−1?? 组成的序列.数组中的元素有可能为正数.负数或 00.你需要从数组中找出元素总和最大的一个连续子序列. 比如,对于数组 1,-3,2,6,-5,81,−3,2,6,−5,8,其最大连续子序列之和是 2+6-5+8=112+6−5+8=11. 对于一段区间内的最大连续

最长公共子序列的代码实现

关于最长公共子序列(LCS)的相关知识,http://blog.csdn.net/liufeng_king/article/details/8500084 这篇文章讲的比较好,在此暂时不再详说. 以下是我代码实现两种方式:递归+递推: 1 #include <bits/stdc++.h> 2 using namespace std; 3 int A[100]; 4 int B[100]; 5 6 //int B[]={2,3,5,6,9,8,4}; 7 int d[100][100]={0};

hdu1231 最大连续子序列

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22849    Accepted Submission(s): 10135 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j