fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.  
  5. int t;
  6. cin>>t;
  7.  
  8. while (t--) {
  9. int n;
  10. cin >> n;
  11. set<int>s;
  12. vector<int>st1;
  13. vector<int>st2;
  14. int a[n];
  15. /// scan
  16. for (int i = 0; i < n; i++) {
  17. cin>>a[i];
  18. s.insert(a[i]);
  19. }
  20. /// push to vector st1
  21. for(auto t: s) st1.push_back(t);
  22.  
  23.  
  24. int ans1=1;
  25. int c1=0,c2=0;
  26. /// set 1
  27. for (int i = 0; i < st1.size()-1; ++i)
  28. {
  29. if(abs(st1[i]-st1[i+1])!=1) { ans1++;}
  30.  
  31.  
  32. }
  33.  
  34. ///set 2
  35.  
  36. for (int i = 0; i < n; ++i)
  37. {
  38. if(s.find(a[i])!=s.end()) {s.erase(s.find(a[i]));}
  39. else st2.push_back(a[i]);
  40. }
  41. int ans2;
  42. if(!st2.empty()) {
  43. ans2=1;
  44.  
  45. sort(st2.begin(), st2.end());
  46.  
  47.  
  48. for (int i = 0; i < st2.size() - 1; ++i) {
  49.  
  50. if(abs(st2[i]-st2[i+1])!=1) {ans2++; }
  51.  
  52.  
  53.  
  54. }
  55.  
  56. }
  57. else ans2=0;
  58.  
  59.  
  60.  
  61. cout<<ans1+ans2<<endl;
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5380KB
stdin
10
6
2 2 3 4 3 1
5
11 8 7 10 9
6
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
8
1 1 4 4 2 3 2 3
6
1 2 3 2 3 4
7
10 11 11 12 12 13 13
7
8 8 9 9 10 10 11
8
4 14 5 15 6 16 7 17
8
5 15 6 14 8 12 9 11
5
4 2 2 3 4
stdout
2
1
6
2
2
2
2
2
4
3