AcWing 1490. 最长上升子串——Java代码版
原题链接
简单
作者:
三玖天下第一
,
2021-02-20 14:02:06
,
所有人可见
,
阅读 451
import java.io.*;
public class 最长上升子串 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(reader.readLine().trim());
String[] temp = reader.readLine().split(" ");
int[] a = new int[n+2], f = new int[n+2], g = new int[n+2];
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(temp[i-1]);
}
for (int i = 1; i <= n; i++) {
if (a[i]> a[i-1]) f[i] = f[i-1]+1;
else f[i] = 1;
}
for (int i = n; i >= 1; i--) {
if(a[i] < a[i+1]) g[i] = g[i+1] +1;
else g[i] = 1;
}
int res = 0;
for (int i = 1; i <= n; i++) {
int left = f[i-1], right = g[i+1];
if (a[i-1] < a[i+1]) res = Math.max(res, left+ right);
else res = Math.max(res, Math.max(left, right));
}
System.out.println(res);
}
}