AcWing 799. 最长连续不重复子序列-java-队列滑动窗口
原题链接
简单
作者:
Astarion
,
2021-03-02 09:34:29
,
所有人可见
,
阅读 231
import java.io.*;
class Main {
static int N = 100010;
static int n, maxLen;
static int[] a = new int[N];
static int head, tail;
static int[] queue = new int[N];
static boolean[] isIn = new boolean[N];
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
n = Integer.parseInt(in.readLine());
String[] strs = in.readLine().split(" ");
for (int i = 1; i <= n; i++) a[i] = Integer.parseInt(strs[i - 1]);
in.close();
isr.close();
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
for (int i = 1; i <= n; i++) {
if (isIn[a[i]]) {
for (int x = queue[head]; x != a[i]; ) {
isIn[x] = false;
x = queue[++head];
}
head++;
}
queue[tail++] = a[i];
isIn[a[i]] = true;
maxLen = Math.max(maxLen, tail - head);
}
out.write(maxLen + "");
out.flush();
out.close();
osw.close();
}
}