fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int trips(int weight[],int num){
  4. unordered_map<int,int>w;
  5. int count;
  6. for(int i=0;i<num;i++){
  7. w[weight[i]]++;
  8. }
  9. int ans=0;
  10. for(auto a:w){
  11. int c=a.second;
  12. if(c==1){
  13. return -1;
  14. }
  15. if(c%3==0){
  16. count=c/3;
  17. }
  18. else{
  19. count=c/3+1;
  20. }
  21. ans=ans+count;
  22. }
  23. return ans;
  24.  
  25. }
  26.  
  27. int main() {
  28. // your code goes here
  29. int n;
  30. cin>>n;
  31. int packageWeight[n];
  32. for(int i=0;i<n;i++){
  33. cin>>packageWeight[i];
  34. }
  35. cout<<"the minimum number of trips are:"<<trips(packageWeight,n);
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5280KB
stdin
7 
1 8 5 8 5 1 1
stdout
the minimum number of trips are:3