算法思想:
朴素的做法(n^2)
类似于合唱队形的朴素做法
可以先预处理从左到右的最大子序列的和f[i]
然后遍历f找到最大值。
朴素做法可以解决最长与最大的上升子序列的问题,但是贪心加二分只能解决最长上升子序列的问题。
import java.util.*;
class Main{
static int n = 0, N = 1010, last = 0;
static int[] nums = new int[N], f = new int[N];
public static void main(String[] args)throws Exception{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1; i <= n; ++i){
nums[i] = sc.nextInt();
f[i] = nums[i];
}
for(int i = 2; i <= n; ++i){
for(int j = 1; j < i; ++j){
if(nums[i] > nums[j]){
f[i] = Math.max(f[i], f[j] + nums[i]);
}
}
}
int max = 0;
for(int i = 1; i <= n; ++i){
max = Math.max(max, f[i]);
}
System.out.print(max);
}
}