fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {ios_base::sync_with_stdio(false);
  5. cin.tie(NULL);
  6. long long q;
  7. cin>>q;
  8. while(q--)
  9. {long long n,sm=0;
  10. cin>>n;
  11. vector<long long>v;
  12. long long a[n+1];
  13. memset(a,0,sizeof(a));
  14. for(int i=0;i<n;i++)
  15. {int x;
  16. cin>>x;
  17. a[x]++;
  18. }
  19. for(int i=0;i<n+1;i++)
  20. {v.push_back(a[i]);
  21. }sort(v.begin(),v.end());
  22. long long cnt=0;
  23. for(auto it=v.end()-1;it!=v.begin()-1;it--)
  24. {
  25. *it-cnt;
  26. if(*it-cnt>0)
  27. sm=sm+*it-cnt;
  28. cnt++;
  29. }
  30. cout<<sm;
  31. v.clear();
  32. cout<<"\n";
  33. }
  34. }
Success #stdin #stdout 0s 15240KB
stdin
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
stdout
3
10
6