[Resource limit]
Time limit: 1.0s Memory limit: 256.0MB
[Problem description]
Student A's academic performance is very unstable, so the teacher said to him: "As long as your grades improve for 4 consecutive days, then I will reward you with a A little red flower." But this was too difficult for classmate A. So, the teacher relaxed his requirements: "As long as your grades are increasing for 4 days, I will reward you with a little red flower." That is, as long as i<j< k<l and for the score wi<wj<wk<wl, then you can get a small red flower reward. Now let you find out, how many red flowers can student A get.
【Input format】
The first line contains an integer n, which means there are n days in total. The second line has n numbers, representing the daily score wi.
[Output format]
A number, indicating how many red flowers can be obtained in total.
[Sample input]
6
1 3 2 3 4 5
[Sample output]
6Data
scale and convention
For 40% of the data, n<=50;
for 100% of the data, n<=2000, 0<=wi<= 10 9 .
——————————————————————————————————————————————————— -
refer to the original text
import java.util.Scanner;
public class Main {
static int[] w;
//dp[i][j] 定义为以w[i]为起点,所有递增序列长度为j的序列的个数。
static long[][] dp = new long[2001][5];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
w = new int[n + 1];
for (int i = 0; i < n; i++) {
w[i] = sc.nextInt();
}
//dp[i][j]= ∑dp[k][j-1] (k>i,a[k]>a[i])
//1 3 2 3 4 5
for (int i = n - 1; i >= 0; i--) {
//因为dp[n-1][1]是显然的,所以从后往前递推
dp[i][1] = 1;
//w[i]是当前序列的头
for (int j = i + 1; j < n; j++) {
//w[j]是第二,对应后面的dp[j][k-1]
if (w[j] > w[i]) {
//k可以理解为当前递增序列的长度
int k = 2;//此时序列长度至少为2,所以初始k=2
while (k <= 4) {
if (dp[j][k - 1] == 0) break;//防止while死循环
//以w[i]为首,长度为k的递增序列的个数 包含 以w[j]为首,长度为k-1的递增序列的个数
dp[i][k] += dp[j][k - 1];
k++;//下次while求解以w[i]为首,长度为k++的递增序列的个数
}
}
}
}
long res = 0;
for (int i = n - 1; i >= 0; i--) {
res += dp[i][4];
}
System.out.println(res);
}
}