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.
给定一个K个整数的序列{N1,N2, ...,NK}。连续子序列被定义为{Ni,Ni+1, ...,Nj其中1≤i≤j≤K。最大子序列是元素之和最大的连续子序列。例如,给定序列{-2,11,-4,13,-5,-2},其最大子序列为{11,-4、13},最大和为20。
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
现在,您应该找到最大和,以及最大子序列的第一个和最后一个数字。
Input Specification:
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.
每个输入文件包含一个测试用例。每个案例占用两行。第一行包含一个正整数K(≤10000)。第二行包含K个数字,用空格分隔。
Output Specification:
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.
对于每个测试用例,在一行中输出最大和,以及最大子序列的第一个和最后一个数字。数字之间必须用一个空格隔开,但在一行的末尾不能有多余的空格。如果最大子序列不是唯一的,则输出索引i和j最小的子序列(如示例所示)。如果所有的K个数字都是负数,那么它的最大和被定义为0,并且你应该输出整个序列的第一个和最后一个数字。
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
最后一个测试点过不了的话,就多提交几遍,时间卡的有点死,刚好198ms左右,没办法,这就是Java的缺点,多提交几遍是能够过的
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
//动态规划
class Main{
public static void main(String[] args)throws IOException {
//Java为了防止超时,必须得换种输入方式,这种输入方式要比scanner快很多
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(bf);
in.nextToken();
int n = (int)in.nval;
String []str = bf.readLine().split(" ");
int []arr = new int[n];
boolean flag = true;
for(int i = 0 ; i < n;i++)
{
arr[i] = Integer.parseInt(str[i]);
if(arr[i] >= 0) flag = false;
}
if(flag == true)
{
System.out.println(0 + " " + arr[0] + " " + arr[n-1]);
return ;
}
int []dp = new int[n];//dp[i]定义为从0到i的位置,数组中最大的子序列和
int []pre = new int[n];//记录从i位置到前面pre[i]位置的最大子序列,前面开始的位置
pre[0] = 0;
dp[0] = arr[0];
int max = -1;
// dp[i]只有两个方向可以推出来:
// 1.dp[i - 1] + nums[i],即:nums[i]加入当前连续子序列和
// 2.nums[i],即:从头开始计算当前连续子序列和
// 一定是取最大的,所以dp[i] = max(dp[i - 1] + nums[i], nums[i]);
for(int i = 1 ; i < n;i++)
{
if(dp[i-1] + arr[i] > arr[i])
{
pre[i] = pre[i-1];
dp[i] = dp[i-1] + arr[i];
}
else
{
pre[i] = i;
dp[i] = arr[i];
}
}
int maxindex = 1;
for(int i = 0 ; i < n ;i++)
{
if(dp[i] > max)
{
max = dp[i];
maxindex = i;
}
}
System.out.println(max + " " + arr[pre[maxindex]] + " " + arr[maxindex] );
}
}