01-复杂度1 最大子列和问题 (20 分)
给定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个随机整数;
输入格式:
输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
输入样例:
6
-2 11 -4 13 -5 -2
输出样例:
20
来源
- 作者: DS课程组
- 单位: 浙江大学
- 时间限制: 50000 ms
- 内存限制: 64 MB
- 代码长度限制: 16
JAVA写法:
java和c还是很像的,最近在练JAVA,就写成java的格式吧。注意,输入N的量比较大,java的Scanner类输入可能会超时,所以在此用了InputStreamReader来输入。
/*
N^3写法
得分16
代码如下:
*/
import java.util.Scanner;
import java.io.*;
public class Main{
private static Reader reader;
public static int get_int()
{
int res = 0;
int read,flag=1;
try {
while((read = reader.read()) != -1)
{
if(read == '-')
{
flag = -1;
}
else if(Character.isDigit(read))
{
res = read - '0';
while((read = reader.read()) != -1)
{
if(Character.isDigit(read)) {
res = res * 10 + (read - '0');
}
else
break;
}
break;
}
}
}catch(IOException e)
{
e.printStackTrace();
}
return flag*res;
}
public static int sequence_get_max(int a[], int N)
{
int MaxSum = 0;
for(int i = 0; i < N; i++ ) { /* i是子列左端位置 */
for(int j = i; j < N; j++ ) { /* j是子列右端位置 */
int ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和 */
for(int k = i; k <= j; k++ )
ThisSum += a[k];
if( ThisSum > MaxSum ) /* 如果刚得到的这个子列和更大 */
MaxSum = ThisSum; /* 则更新结果 */
} /* j循环结束 */
} /* i循环结束 */
return MaxSum;
}
public static void main(String args[]) {
reader = new InputStreamReader(System.in);
int n = get_int();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = get_int();
}
System.out.println(sequence_get_max(a,n));
}
}
/*
N^2写法
得分:20
代码如下:
*/
import java.util.Scanner;
import java.io.*;
public class Main{
private static Reader reader;
public static void main(String args[]) {
reader = new InputStreamReader(System.in);
int n = get_int();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = get_int();
}
System.out.println(sequence_get_max(a,n));
}
public static int get_int()
{
int res = 0;
int read,flag=1;
try {
while((read = reader.read()) != -1)
{
if(read == '-')
{
flag = -1;
}
else if(Character.isDigit(read))
{
res = read - '0';
while((read = reader.read()) != -1)
{
if(Character.isDigit(read)) {
res = res * 10 + (read - '0');
}
else
break;
}
break;
}
}
}catch(IOException e)
{
e.printStackTrace();
}
return flag*res;
}
public static int sequence_get_max(int a[], int N)
{
int MaxSum = 0;
for(int i = 0; i < N; i++ ) { /* i是子列左端位置 */
int ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和 */
for(int j = i; j < N; j++ ) { /* j是子列右端位置 */
ThisSum += a[j];
if( ThisSum > MaxSum ) /* 如果刚得到的这个子列和更大 */
MaxSum = ThisSum; /* 则更新结果 */
} /* j循环结束 */
} /* i循环结束 */
return MaxSum;
}
}
对于另外两种更简单的算法,如下
分而治之
import java.util.Scanner;
import java.io.*;
public class Main{
private static Reader reader;
public static void main(String args[]) {
reader = new InputStreamReader(System.in);
int n = get_int();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = get_int();
}
System.out.println(sequence_get_max(a,0,n-1));
}
public static int get_int()
{
int res = 0;
int read,flag=1;
try {
while((read = reader.read()) != -1)
{
if(read == '-')
{
flag = -1;
}
else if(Character.isDigit(read))
{
res = read - '0';
while((read = reader.read()) != -1)
{
if(Character.isDigit(read)) {
res = res * 10 + (read - '0');
}
else
break;
}
break;
}
}
}catch(IOException e)
{
e.printStackTrace();
}
return flag*res;
}
public static int sequence_get_max(int a[], int low, int high)
{
int max_sum = 0;
if(low == high)
{
return a[low];
}
int l_sum = sequence_get_max(a,low, (high+low)/2);
// System.out.println(l_sum);
if(l_sum > max_sum)
max_sum = l_sum;
int r_sum = sequence_get_max(a,(high+low)/2+1,high);
// System.out.println(r_sum);
if(r_sum > max_sum)
max_sum = r_sum;
/*求中间位置的最大值*/
int this_left = 0, this_right = 0;
int ml_sum = a[(high+low)/2],mr_sum = a[(high+low)/2+1];
for(int i = (high+low)/2; i >= low; i--)
{
this_left += a[i];
if(this_left > ml_sum)
ml_sum = this_left;
}
for(int i = (high+low)/2 + 1; i <= high; i++)
{
this_right += a[i];
if(this_right > mr_sum)
mr_sum = this_right;
}
if((ml_sum + mr_sum) > max_sum)
max_sum = ml_sum + mr_sum;
// System.out.println(ml_sum + mr_sum);
return max_sum;
}
}
在线算法
import java.util.Scanner;
import java.io.*;
public class Main{
private static Reader reader;
public static void main(String args[]) {
reader = new InputStreamReader(System.in);
int n = get_int();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = get_int();
}
System.out.println(sequence_get_max4(a,n));
}
public static int get_int()
{
int res = 0;
int read,flag=1;
try {
while((read = reader.read()) != -1)
{
if(read == '-')
{
flag = -1;
}
else if(Character.isDigit(read))
{
res = read - '0';
while((read = reader.read()) != -1)
{
if(Character.isDigit(read)) {
res = res * 10 + (read - '0');
}
else
break;
}
break;
}
}
}catch(IOException e)
{
e.printStackTrace();
}
return flag*res;
}
public static int sequence_get_max4(int a[], int n)
{
int max_sum = 0;
int this_sum = 0;
for(int i = 0; i < n; i++)
{
this_sum += a[i];/*向右累加*/
if(this_sum > max_sum)/*找到更大的累加和,更新*/
max_sum = this_sum;
else if(this_sum < 0)/*当前和不能使累加和增加,则丢弃*/
this_sum = 0;
}
return max_sum;
}
}