fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int MAXN = 200000;
  7.  
  8. int last_occurrence[MAXN + 10];
  9.  
  10. int main() {
  11. ios_base::sync_with_stdio(false);
  12. cin.tie(NULL);
  13. int t;
  14. cin >> t;
  15. while (t--) {
  16. int n;
  17. cin >> n;
  18. vector<int> a(n);
  19. for (int i = 0; i < n; i++) {
  20. cin >> a[i];
  21. }
  22. for (int i = 1; i <= n; i++) {
  23. last_occurrence[i] = -1;
  24. }
  25. for (int i = n - 1; i >= 0; i--) {
  26. if (last_occurrence[a[i]] == -1) {
  27. last_occurrence[a[i]] = i;
  28. }
  29. }
  30.  
  31. int segments = 0;
  32. int i = 0;
  33. while (i < n) {
  34. int bound = last_occurrence[a[i]];
  35. int j = i;
  36. while (j <= bound) {
  37. if (last_occurrence[a[j]] > bound) {
  38. bound = last_occurrence[a[j]];
  39. }
  40. j++;
  41. }
  42. segments++;
  43. i = bound + 1;
  44. }
  45. cout << segments << '\n';
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5316KB
stdin
8
6
1 2 2 3 1 5
8
1 2 1 3 2 1 3 2
5
5 4 3 2 1
10
5 8 7 5 8 5 7 8 10 9
3
1 2 2
9
3 3 1 4 3 2 4 1 2
6
4 5 4 5 6 4
8
1 2 1 2 1 2 1 2
stdout
2
1
5
3
2
1
1
1