最长连续不重复子序列

Myth丶恋晨 2024-03-30 11:36 158阅读 0赞

最长连续不重复子序列

给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。

输入格式
第一行包含整数 n。

第二行包含 n 个整数(均在 0∼105 范围内),表示整数序列。

输出格式
共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。

数据范围
1≤n≤105
输入样例:
5
1 2 2 3 5
输出样例:
3

提交代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 100010;
  4. int a[N], s[N];
  5. int n, res;
  6. int main()
  7. {
  8. cin >> n;
  9. for (int i = 0; i < n; ++ i) cin >> a[i];
  10. for (int i = 0, j = 0; i < n; ++ i)
  11. {
  12. s[a[i]] ++; // 记录下a[i]出现的次数
  13. while(s[a[i]] > 1) // 一点碰见两个重复的元素后
  14. {
  15. s[a[j]] --; // 这里要主要的一点是这个算法是没有回溯的
  16. // 不要被for循环里面的条件误导以为会回溯、
  17. // 现在遇到两个相同的元素了
  18. // !!! 现在是这个算法最厉害的地方
  19. // 这个j代表的是 j可以到达最左的地方 所以在j左边的
  20. // 元素的个数就需要都-- 这点很妙
  21. // 每次求的是 j到i之间的符合条件的最大值
  22. j ++; // 然后j++
  23. }
  24. res = max(res, i - j + 1); // 这个res的含义是 在i这个位置、
  25. // 可以达到的符合题目条件的最大长度
  26. }
  27. cout << res;
  28. return 0;
  29. }
  30. import java.io.*;
  31. import java.util.*;
  32. public class Main
  33. {
  34. public static void main(String[] args) throws IOException{
  35. Scanner in = new Scanner(System.in);
  36. int n = in.nextInt();
  37. int [] a = new int [n + 10];
  38. int [] s = new int [n + 10];
  39. int res = 0;
  40. for (int i = 0; i < n; ++ i) a[i] = in.nextInt();
  41. for (int i = 0, j = 0; i < n; ++ i)
  42. {
  43. s[a[i]] ++;
  44. while(s[a[i]] > 1)
  45. {
  46. s[a[j]] --;
  47. j ++;
  48. }
  49. res = Math.max(res, i - j + 1);
  50. }
  51. System.out.println(res);
  52. }
  53. }

合并集合

一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。

现在要进行 m 个操作,操作共有两种:

M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作;
Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中;
输入格式
第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。

输出格式
对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 No。

每个结果占一行。

数据范围
1≤n,m≤105
输入样例:
4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4
输出样例:
Yes
No
Yes

提交代码

  1. #include<iostream>
  2. using namespace std;
  3. const int N = 100010;
  4. int n, m;
  5. int p[N];
  6. int find(int x) // 找到x的祖先节点
  7. {
  8. if (p[x] != x) p[x] = find(p[x]);
  9. return p[x];
  10. }
  11. int main()
  12. {
  13. scanf("%d %d", &n, &m);
  14. for (int i = 1; i <= n; ++i) p[i] = i;
  15. while (m--)
  16. {
  17. char op;
  18. int a, b;
  19. scanf (" %c%d%d", &op, &a, &b);
  20. if (op == 'M') p[p[find(a)]] = find(b); // 让a的祖先节点指向b的祖先节点
  21. else
  22. {
  23. if (find(a) == find(b)) puts("Yes");
  24. else puts("No");
  25. }
  26. }
  27. return 0;
  28. }
  29. import java.io.*;
  30. public class Main
  31. {
  32. static int N = 100010;
  33. static int n, m;
  34. static int [] p = new int [N];
  35. static int find(int x)
  36. {
  37. if (p[x] != x) p[x] = find(p[x]);
  38. return p[x];
  39. }
  40. public static void main(String[] args) throws IOException
  41. {
  42. BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
  43. String [] str = reader.readLine().split(" ");
  44. n = Integer.parseInt(str[0]);
  45. m = Integer.parseInt(str[1]);
  46. for (int i = 1; i <= n; ++ i) p[i] = i;
  47. while (m -- > 0)
  48. {
  49. String op;
  50. int a, b;
  51. str = reader.readLine().split(" ");
  52. op = str[0];
  53. a = Integer.parseInt(str[1]);
  54. b = Integer.parseInt(str[2]);
  55. if (op.equals("M")) p[find(a)] = find(b);
  56. else
  57. {
  58. if (find(a) == find(b)) System.out.println("Yes");
  59. else System.out.println("No");
  60. }
  61. }
  62. }
  63. }

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读

    相关 连续重复序列

    最长连续不重复子序列 给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。 输入格式 第一行包含整数 n。 第二行包含 n 个整数

    相关 连续重复序列

    题目描述 给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。 输入格式 第一行包含整数 n。 第二行包含 n 个整数(均在 0∼1

    相关 799. 连续重复序列

    给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。 输入格式 第一行包含整数 n。 第二行包含 n 个整数(均在 0∼105 范围内)

    相关 上升连续序列

    给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。) 注...