这个题没做出来就离谱,下午做完LCCUP脑子就不转了,和上周的情况一模一样
T3 4412. 构造数组
思路:
模拟,更新最右值
import java.util.*;
import java.io.*;
public class Main {
static final int N = 200010, MOD = 998244353;
static Map<Integer, Integer> left = new HashMap<>();
static Map<Integer, Integer> right = new HashMap<>();
static int n;
static int[] a = new int[N];
public static void main(String[] sss) throws IOException{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n; i++) {
if (left.get(a[i]) == null)
left.put(a[i], i);
}
for (int i = n - 1; i >= 0; i--) {
if (right.get(a[i]) == null)
right.put(a[i], i);
}
long res = 1;
int i = 0;
while (i < n) {
int x = left.get(a[i]), y = right.get(a[i]);
if (x == i) {
if (i != 0)
res = (2 * res) % MOD;
}
int ne = y + 1;
for (int j = i; j < ne; j++)
ne = Math.max(ne, right.get(a[j]) + 1);
i = ne;
}
System.out.println(res);
// bw.close();
// br.close();
}
}
类似的题:
763. 划分字母区间