01 複雜度1:最大子列和問題 Description: 給定K個整數組成的序列{N1, N2, ..., Nk},“連續子列”被定義為{Ni, Ni+1, ..., Nj},其中1≤i≤j≤K。“最大子列和”則被定義為所有連續子列元素的和中最大者。例如給定序列{ 2, 11, ...
01-複雜度1:最大子列和問題
Description:
給定K個整數組成的序列{N1, N2, ..., Nk},“連續子列”被定義為{Ni, Ni+1, ..., Nj},其中1≤i≤j≤K。“最大子列和”則被定義為所有連續子列元素的和中最大者。例如給定序列{-2, 11, -4, 13, -5, -2},其連續子列{11, -4, 13}有最大的和20。現要求你編寫程式,計算給定整數序列的最大子列和。
本題旨在測試各種不同的演算法在各種數據情況下的表現。各組測試數據特點如下:
數據1:與樣例等價,測試基本正確性;
數據2:102個隨機整數;
數據3:103個隨機整數;
數據4:104個隨機整數;
數據5:105個隨機整數。
Input:
輸入第1行給出正整數K(≤100000);第2行給出K個整數,其間以空格分隔。
Output:
在一行中輸出最大子列和。如果序列中所有整數皆為負數,則輸出0。
SampleInput:
6
-2 11 -4 13 -5 -2
SampleOutput:
20
Codes:
//#define LOCAL
#include <cstdio>
#define M 100010
int A[M];
int maxV(int a, int b, int c) {
int t = a>b?a:b;
return t = t>c?t:c;
}
int mSub(int l, int r) {
if(l == r) return A[l];
int a, b, c, d, i, u, v, m = (l+r)/2;
a = b = c = d = 0, u = mSub(l, m), v = mSub(m+1, r);
for(i=m; i>=l; --i) {
a += A[i];
if(a > c) c = a;
}
for(i=m+1; i<=r; ++i) {
b += A[i];
if(b > d) d = b;
}
return maxV(u, v, c+d);
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif
int i, n, sum = 0;
scanf("%d", &n);
for(i=0; i<n; ++i) {
scanf("%d", &A[i]);
if(A[i] < 0) ++sum;
}
if(sum == n) printf("0\n");
else printf("%d\n", mSub(0, n-1));
return 0;
}
PAT-1007:Maximum Subsequence Sum.
Description:
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.
Output:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
SampleInput:
10
-10 1 2 3 4 -5 -23 3 7 -21
SampleOutput:
10 1 4
Codes:
//#define LOCAL
#include <cstdio>
#define M 10010
int s, p, q, A[M];
void mSub(int n) {
int a = 0, b = 0, i;
for(i=0; i<n; ++i)
if(A[i] >= 0) { p = q = i; break; }
for(i=0; i<n; ++i) {
a += A[i];
if(a > s) { s = a; q = i; p = b; }
if(a < 0) { a = 0; b = i+1; }
}
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif
int i, n, f = 1;
scanf("%d", &n);
for(i=0; i<n; ++i) {
scanf("%d", &A[i]);
if(A[i] >= 0) f = 0;
}
mSub(n);
if(f) { s = 0; p = 0; q = n-1; }
printf("%d %d %d\n", s, A[p], A[q]);
return 0;
}