fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb emplace_back
  4.  
  5. int main()
  6. {
  7.  
  8. int t = 1;
  9. cin >> t;
  10. while (t--)
  11. {
  12.  
  13. int n, i, x, ok = 0;
  14. cin >> n;
  15.  
  16. multiset<int> s;
  17.  
  18. for (i = 0; i < n; i++)
  19. {
  20. cin >> x;
  21. s.insert(x);
  22. }
  23.  
  24. int op = 0;
  25.  
  26. for (i = 1; i <= n; i++)
  27. {
  28. if (s.find(i) != s.end())
  29. s.erase(s.find(i));
  30.  
  31. else
  32. {
  33. auto it = s.begin();
  34. x = *it;
  35.  
  36. if (x < i)
  37. op++;
  38.  
  39. else
  40. {
  41. ok = 1;
  42. break;
  43. }
  44.  
  45. s.erase(it);
  46. }
  47. }
  48.  
  49. if (ok == 1)
  50. op = -1;
  51.  
  52. cout << op << endl;
  53.  
  54.  
  55. }
  56. }
Success #stdin #stdout 0s 5660KB
stdin
 3 
 4
 1 3 3 4
 5
 1 2 3 5 4
 5
 2 3 2 1 3
stdout
-1
0
2