这个题没做出来就离谱,下午做完LCCUP脑子就不转了,和上周的情况一模一样

T3 4412. 构造数组

image.png

思路:
模拟,更新最右值

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. static final int N = 200010, MOD = 998244353;
  5. static Map<Integer, Integer> left = new HashMap<>();
  6. static Map<Integer, Integer> right = new HashMap<>();
  7. static int n;
  8. static int[] a = new int[N];
  9. public static void main(String[] sss) throws IOException{
  10. // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11. // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  12. Scanner sc = new Scanner(System.in);
  13. n = sc.nextInt();
  14. for (int i = 0; i < n; i++)
  15. a[i] = sc.nextInt();
  16. for (int i = 0; i < n; i++) {
  17. if (left.get(a[i]) == null)
  18. left.put(a[i], i);
  19. }
  20. for (int i = n - 1; i >= 0; i--) {
  21. if (right.get(a[i]) == null)
  22. right.put(a[i], i);
  23. }
  24. long res = 1;
  25. int i = 0;
  26. while (i < n) {
  27. int x = left.get(a[i]), y = right.get(a[i]);
  28. if (x == i) {
  29. if (i != 0)
  30. res = (2 * res) % MOD;
  31. }
  32. int ne = y + 1;
  33. for (int j = i; j < ne; j++)
  34. ne = Math.max(ne, right.get(a[j]) + 1);
  35. i = ne;
  36. }
  37. System.out.println(res);
  38. // bw.close();
  39. // br.close();
  40. }
  41. }

类似的题:
763. 划分字母区间