题目描述
注意!!!!
不需要连续!!!
题解看代码注释
Java 代码
import java.util.*;
import java.io.*;
public class Main{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static StreamTokenizer in = new StreamTokenizer(reader);
static int getInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args) throws IOException{
int n = getInt();
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
//s1 的值 为 1111
//s2 的值 为 11112222
//s3 的值 为 11112222111 他反转的值为 11111112222
//s4 的值 为 111112221112222 他反转的值为111111112222222
// 111 222 111 222 分别对应s1 s2 s3 s4 这样最大的范围
//1 只有可能放在 s1 和 s2尾巴 s3尾巴
//2 同样的道理
while(n-->0){
int x = getInt();
if(x == 1){
s1 = s1+1;
s3 = Math.max(s2+1,s3+1);
}else{
s2 = Math.max(s1+1,s2+1);
s4 = Math.max(s3+1,s4+1);
}
}
System.out.println(Math.max(s3,s4));
}
}