Max Sequence(dp最大m子段和)
Max Sequence(dp最大m子段和)
Problem Max Sequence
Time limit 3000 ms
Mem limit 65536 kB
OS Linux
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
output
40
翻译的大意:
给你一堆测试样例,输出他们之间和的最大的结果。
分析:这是dp里面比较典型的最大的m子段和
最大的m子段和:给定由n个整数(可能为负)组成的序列a1、a2、a3…,an, 以及一个正整数m,要求确定序列的m个不相交子段,使这m个子段的总和最大!
ac代码
#include<cstring>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>//习惯写很多头文件,vj上不能用万能头,所以写的多
using namespace std;//最大m子段问题。
const int N = -0x3fffffff;
int num[100001], dp[100001];
int main()
{
int n, max1, sum, s;
while (scanf("%d", &n), n)
{
sum = 0, max1 = N;
for (int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
sum += num[i];
if (sum > max1)
max1 = sum;
dp[i] = max1;
if (sum < 0)
sum = 0;
}//读数据时,正向一次dp,得到第i个元素的最大值
dp[0] = s = max1=N;
sum = 0;
for (int i = n; i > 0; i--)//反向一次
{
sum += num[i];
if (sum > max1)
max1 = sum;
if (s < max1 + dp[i - 1])
s = max1 + dp[i - 1];//加上前面的得到最大值
if (sum < 0)
sum = 0;
}
printf("%d\n", s);
}
}

浙公网安备 33010602011771号