poj2593 Max Sequence(求两个不相交最大字段和)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=2593

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).

You should output S.

Input

The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

5
-5 9 -5 11 20
0

Sample Output

40

思想:对于数据a[],从左向右依次求解以a[i]结尾的最大子段和b[i],

然后,从右向左遍历,求a[i]右边(包括a[i])的最大子段和sum,输出sum+b[i-1]的  最大值。

代码如下:

#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define M 100000+17
int a[M],b[M];
int main()
{
	int n,i;
	while(cin >> n && n)
	{
		int sum = 0, MAX = -INF;
		for(i = 1; i <= n; i++)
		{
			cin >> a[i];
			sum+=a[i];
			if(sum > MAX)
			{
				MAX = sum;
			}
			b[i] = MAX;
			if(sum < 0)
			{
				sum = 0;
			}
		}
		MAX = -INF;
		sum = 0;
		int ans = MAX, t;
		for(i = n; i > 1; i--)
		{
			sum+=a[i];
			if(sum > MAX)
			{
				MAX = sum;
			}
			t = MAX + b[i-1];
			if(t > ans)
			{
				ans = t;
			}
			if(sum < 0)
			{
				sum = 0;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

poj2593 Max Sequence(求两个不相交最大字段和)

时间: 2024-11-05 12:20:57

poj2593 Max Sequence(求两个不相交最大字段和)的相关文章

poj2479 &amp;&amp; poj2593Maximum sum(求两个不相交最大字段的和)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2479 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists of T

POJ-2593 Max Sequence

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16001   Accepted: 6715 Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should output S. Input The input will consist of several test cases. Fo

【原题】求两个不相交的连续子数组的最大和

题目: 有一个整数数组n,a和b是n里两个互不相交的子数组.返回sum(a)+sum(b)的最大值. 分析: 新建两个数组left和right,left[i]表示n[0:i]的连续子数组的最大和,right[i]表示n[i:length-1]的连续子数组的最大和.left[i]+right[i+1]的最大值就是答案. int SumOfTwoSubarray(const vector<int> &n) { if (n.size() < 2) { throw exception()

C语言求两个函数中的较大者的MAX函数

//求两个函数中的较大者的MAX函数 #include <stdio.h> int main(int argc, const char * argv[]) { printf("input two nimbers\n"); int max(int x,int y); int a, b,c; scanf("%d,%d,",&a,&b); c=max(a,b); printf("max=%d\n",c); printf(&q

[ACM] POJ 2593 Max Sequence (动态规划,最大字段和)

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15569   Accepted: 6538 Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should output S. Input The input will consist of several test cases. Fo

POJ 2593&amp;&amp;2479:Max Sequence

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16329   Accepted: 6848 Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should output S. Input The input will consist of several test cases. Fo

求两个数的最大公约数和最小公倍数

import java.util.Scanner; //求两个数的最大公约数,最小共倍数. public class CommonMaxDivisor { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int m=scanner.nextInt(); int n=scanner.nextInt(); scanner.close(); CommonMaxDivisor cmd=new

求两个数之间的随机正整数

求两个数之间的随机正整 数.并添到新数组,数组长度由自己指定.并且数组中不能有重复的值 function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } function numInt(n, min, max) { const arr = [] while(arr.length < n) { let num = getRandomInt(min, max) if (arr.i

求两圆相交部分面积(C++)

已知两圆圆心坐标和半径,求相交部分面积: 1 #include <iostream> 2 using namespace std; 3 #include<cmath> 4 #include<stdio.h> 5 #define PI 3.141593 6 struct point//点 7 { 8 double x,y; 9 }; 10 struct circle//圆 11 { 12 point center; 13 double r; 14 }; 15 float